Last active
January 17, 2017 19:26
-
-
Save chr33s/69eee6e9af39f42c0ba0da1d3226d1f3 to your computer and use it in GitHub Desktop.
server.http.js
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
| '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