For each letter in the CRUD acronym the HTTP Methods are as follows:
"Routing" means, we want to handle requests to different URLs differently
var http = require("http");
var handler = require("./handler");
var port = 4000;
//*** List of Routes and Associated Handler Functions ***//
var routes = {}
routes["/"] = handler.home;
routes["/create"] = handler.create;
routes["/update"] = handler.update;
//*** Invokes the right handler or throws error ***//
var router = function(req, res){
var url = req.url;
console.log("request received for ", url);
if (typeof routes[url] === 'function'){
routes[url](req, res);
} else {
console.log('Error, route for ', url, 'does not exist');
res.writeHead(404, {"Content-Type": "text/plain"});
res.end(" ERROR!!");
}
}
http.createServer(router).listen(port);
console.log('Server running on port', port);
module.exports = {
home: function handler(req, res) {
// console.log("Request for " + pathname + " received.");
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("Welcome");
res.end(" to Testing");
},
create: function handler(req, res) {
// console.log("Request for " + pathname + " received.");
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("Welcome");
res.end(" to create article");
},
update: function handler(req, res) {
// console.log("Request for " + pathname + " received.");
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("Welcome");
res.end(" to update article");
},
//createArticle
//updateArticle
//deleteArticle
//viewArticle
}
};
The Node Beginner's Book - http://www.acfo.org/www/uploads/documents/33e2d962a6da1d1124e7c23a9fb23972.pdf
Vanilla JS Node Routing Demo - http://www.learnallthenodes.com/episodes/3-beginning-routing-in-nodejs
Restful API Design With Node JS Restify (more helpful next week when we introduce frameworks) - http://code.tutsplus.com/tutorials/restful-api-design-with-nodejs-restify--cms-22637