Last active
June 22, 2023 15:15
-
-
Save isurfer21/bc52c92800df4c4ecc37b731874cc8c6 to your computer and use it in GitHub Desktop.
Sample minimal static server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
@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