Skip to content

Instantly share code, notes, and snippets.

@w7089
Created Jan 10, 2019
Embed
What would you like to do?
httpServer.js
server = require('http').createServer()
const fs = require('fs');
const data = {'hi' : 'buy'}
server.on('request', (req, res) => {
switch (req.url) {
case '/api':
res.writeHead(200, {'Content-Type' : 'application-json' })
res.end(JSON.stringify(data))
break;
case '/home':
case '/about':
res.writeHead(200, {'content-type' : 'text/plain'})
res.end(fs.readFileSync(`.${req.url}.html`));
case '/':
res.writeHead(301, {'Location' : '/home'})
res.end()
break
default:
res.writeHead(404)
res.end()
}
// res.write('Hello world\n')
// // req: req.IncomingMessage
// // res: http.ServerResponse
// setTimeout(function() {
// res.write('another hello\n')
// }, 2000)
// setTimeout(function() {
// res.write('2 another hello\n')
// }, 2000)
})
server.listen(8000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment