how to get the url parameters from http

D

devnull

Guest
I am working in a very rudimentary "routing" system for small CMS in nodejs without express or any framework. My aim is to have very few dependencies. For templating I found jrender that works fine in the sample route "hey" below:

var http = require('http')
var jsrender = require ('jsrender');

var html = jsrender.renderFile('./templates/hey.html', {name: "Jim", age: "22"});


http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'}); // http header

var url = req.url;
if(url ==='/about'){
console.log (req.url)
res.write("hey"); //write a response
res.end(); //end the response

}else if(url ==='/contact'){
res.write('<h1>contact us page<h1>'); //write a response
res.end(); //end the response

}else if(url ==='/hey'){
res.write(html); //write a response
res.end(); //end the response

}else{
res.write('<h1>Hello World!<h1>'); //write a response
res.end(); //end the response
}

}).listen(3000, function(){
console.log("Judge Dress live on port 3000"); //the server object listens on port 3000
});


My problem is to get a parameter for a page e.g. /?pages=pagename to have dynamic routes. Is there any way to extact this parameter from req.url ?

Continue reading...
 
Top