Created
March 22, 2017 08:39
-
-
Save giltayar/dd10191d17892c7b48b649bf99458674 to your computer and use it in GitHub Desktop.
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
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