Skip to content

Instantly share code, notes, and snippets.

@chr33s
Last active January 17, 2017 19:26
Show Gist options
  • Select an option

  • Save chr33s/69eee6e9af39f42c0ba0da1d3226d1f3 to your computer and use it in GitHub Desktop.

Select an option

Save chr33s/69eee6e9af39f42c0ba0da1d3226d1f3 to your computer and use it in GitHub Desktop.
server.http.js
'use strict'
const {contentType} = require('mime-types')
const {createServer} = require('http')
const {extname} = require('path')
const {readFile} = require('fs')
const server = createServer(function (req, res) {
if (req.method === 'HEAD' || req.method === 'GET') {
const file = req.url.substr(1) || 'index.html'
readFile(`${__dirname}/public/${file}`, 'binary', function (err, bin) {
if (err) res.writeHead(404)
else {
const ext = contentType(extname(file)) || 'application/octet-stream'
res.writeHead(200, {'Content-Type': ext })
res.write(bin, 'binary')
}
res.end()
})
} else {
res.writeHead(405)
res.end()
}
})
if (!module.parent) server.listen(3000)
module.exports = server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment