Skip to content

Instantly share code, notes, and snippets.

@w7089
Created January 10, 2019 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w7089/313ee6ab5c654239b01c7449bb546c1f to your computer and use it in GitHub Desktop.
Save w7089/313ee6ab5c654239b01c7449bb546c1f to your computer and use it in GitHub Desktop.
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