1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| var http = require('http'); var fs = require('fs');
function startServer () { var onRequest = function(request, response) { if (request.url === '/' || request.url === '/home') { response.writeHead(200, { 'Content-Type': 'text/html'}); fs.createReadStream(__dirname + '/index.html', 'utf8').pipe(response); } else if (request.url === '/review') { response.writeHead(200, { 'Content-Type': 'text/html'}); fs.createReadStream(__dirname + '/review.html', 'utf8').pipe(response); } else if (request.url === '/api/v1/records') { response.writeHead(200, { 'Content-Type': 'application/json' }); var jsonObj = { name: 'sfafas', job: 'coder' } response.end(JSON.stringify(jsonObj)); } else { response.writeHead(200, {'Content-Type': 'text/html'}); fs.createReadStream(__dirname + '/404.html', 'utf8').pipe(response); } } var server = http.createServer(onRequest); server.listen(3000); }
console.log('finished'); module.exports.startServer = startServer;
|