Last active
July 22, 2016 08:53
-
-
Save nickylimjj/da7e26064b66de53fadaea04adca4f64 to your computer and use it in GitHub Desktop.
Nodejs: simple json server
This file contains 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.js | |
var http = require('http') | |
http.createServer( function (req, res) { | |
res.writeHead(200, { 'Content-Type': 'application/json' }) | |
var obj = { | |
firstname: 'John', | |
lastname: 'Lee' | |
} | |
// serialize - convert object to transferrable data to be consumed in API | |
res.end(JSON.stringify(obj)) | |
}).listen(8000) |
This file contains 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
// stream-server.js | |
var http = require('http') | |
var fs = require('fs') | |
http.createServer(function (req, res) { | |
res.writeHead(200, { 'Content-Type': 'application/json' }) | |
// | |
fs.createReadStream(__dirname + '/index.htm').pipe(res) | |
}).listen(8000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Structure of a URL:
scheme://host:port/path?query
For routing, integrate conditionals for data in
req.url
(which refers to the path)