Skip to content

Instantly share code, notes, and snippets.

@nickylimjj
Last active July 22, 2016 08:53
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 nickylimjj/da7e26064b66de53fadaea04adca4f64 to your computer and use it in GitHub Desktop.
Save nickylimjj/da7e26064b66de53fadaea04adca4f64 to your computer and use it in GitHub Desktop.
Nodejs: simple json server
// 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)
// 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)
@nickylimjj
Copy link
Author

Structure of a URL:
scheme://host:port/path?query

For routing, integrate conditionals for data in req.url (which refers to the path)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment