Skip to content

Instantly share code, notes, and snippets.

@giltayar
Created March 22, 2017 08:39
Show Gist options
  • Save giltayar/dd10191d17892c7b48b649bf99458674 to your computer and use it in GitHub Desktop.
Save giltayar/dd10191d17892c7b48b649bf99458674 to your computer and use it in GitHub Desktop.
const http = require('http')
const fs = require('fs')
const path = require('path')
const extensionToContentType = {
'.js': 'application/javascript',
'.html': 'text/html'
}
http.createServer((req, res) => {
if (req.url.startsWith('/public')) {
const file = path.join('public', req.url.substring('/static'.length))
res.setHeader('Content-Type', extensionToContentType[path.extname(file)])
fs.createReadStream(file).pipe(res)
}
else {
res.statusCode = 404
res.end()
}
}).listen(process.env.PORT || 3000, () => console.log('listening...'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment