Reputation: 11
I m using following code for creating server with node js but whenever i run local host on this port in browser it always show index.html file. What is wrong i do ..
var http = require('http');
var fs = require('fs');
var index = fs.readFileSync('index.html');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'html'});
res.end(index);
}).listen(9615);
Upvotes: 1
Views: 411
Reputation: 7784
If you come from a php background, you may expect that creating a file on the server makes it accessible throught an url containing its file name.
In node.js, things work differently. You must use a routing system. Basically, you are going to tell that the "route" (url) views/about is the file about.html.
I think that the best option for you is to install Express framework : it will create basic routes for you, then you'll just have to copy and change some lines to set up new ones.
Express is also going to make a lot of other stuff easier and faster to develop
Have a look at this 5 min Express tutorial
Upvotes: 0
Reputation:
What you want is to create a router.
Check this: http://www.nodebeginner.org/
Upvotes: 2