问题描述:

I need to develop a web app for admin panel using node.js.I just learnt how to display a static html using node.js.

var sys = require("sys"),

my_http = require("http");

fs = require('fs');

my_http.createServer(function(request,response){

fs.readFile("index.html",function(err,data){

if(err){

console.log("error occured");

}

response.writeHead(200, {'Content-Type': 'text/html','Content- Length':data.length});

response.write(data);

response.end();

});

}).listen(7777);

sys.puts("Server Running on 7777");

But I am not able to understand how to change the view from one html page to another, like when the login button is clicked. I've used to php earlier where I give the name of the php file in the action of the form. Similarly what should be done in node.js?

网友答案:

First of all,

For node.js, everything from routing to redirection is to be hand-coded. You need to learn more in node.js for this. You can refer - http://www.nodebeginner.org/ site for learning.

Also if you want to use some high level library , you can go through - expressjs - http://expressjs.com/starter/basic-routing.html and may google for node express applications.

For you question -

You have to extract information from req.url property and then branch your code accordingly for file reading.

More of this can be found in

http://nodejs.org/api/http.html#http_message_url

Hope it will help you.

相关阅读:
Top