Skip to content

Instantly share code, notes, and snippets.

@isurfer21
Last active June 22, 2023 15:15
Show Gist options
  • Save isurfer21/bc52c92800df4c4ecc37b731874cc8c6 to your computer and use it in GitHub Desktop.
Save isurfer21/bc52c92800df4c4ecc37b731874cc8c6 to your computer and use it in GitHub Desktop.
Sample minimal static server
/*
@title Sample minimal static server
@file static-server.js
@setup npm install connect serve-static
@usage node static-server.js
*/
const os = require('os');
const path = require('path');
const connect = require('connect');
const serveStatic = require('serve-static');
const host = '0.0.0.0';
const port = '8080';
const servePath = path.join(__dirname, 'htdocs');
const getAddresses = () => Object.values(os.networkInterfaces())
.flat()
.filter(({ family }) => family === 'IPv4')
.map(({ address }) => address);
const app = connect();
app.use('/docs/v1/', serveStatic(servePath));
app.listen(port, host, 0, () => {
const servingAt = getAddresses().map(ip => `${ip}:${port}`);
const servingDir = path.relative(process.cwd(), servePath);
console.log(`Server has started & serving '${servingDir}' at ${servingAt.join(', ')}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment