Created
January 10, 2019 12:31
-
-
Save w7089/313ee6ab5c654239b01c7449bb546c1f to your computer and use it in GitHub Desktop.
httpServer.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
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