Handling Requests
Video Course on Real-Time HTML5 with Node.js
continued from: Node as a Webserver
continued from: Node as a Webserver
function handleHTTP(req,res){
res.writeHead(200, {"Content-type": "text/plain"});
res.end("Hello World:");
}
to demonstrate that these are in fact real streams:
function handleHTTP(req,res){
res.writeHead(200, {"Content-type": "text/plain"});
res.write("Hello World:");
res.end(Math.random().toString());
}
Routing
the core level, writing it yourself without a framework like Express
the
req
parameter has lots of properties that you can use for the routing e.g:function handleHTTP(req, res){
if (req.method === "GET") {
// GET route
// res.writeHead(200...
}
} else { res.writeHead(403) }
if you want to use by path, then add
if(req.url === "/"){//send response...}
you can use
if
statements, switch
statements, JSON
config files to create your routes, or use express and connect he doesn't use any middleware framework, because it's simple enough that he doesn't need