There are many tutorials for server nodejs + express. You can simply write res.render(somefile) and express show the html page. but how to do it in plain nodejs, how to render pages? I can't find the answer at http://www.nodebeginner.org/
I wrote simple server, but don't know how fix it for my needs:
var http = require("http");
var url = require("url");
var fs = require("fs");
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
var filename = 'gallery/index.html';
fs.readFile(filename, function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.close();
return;
}
response.writeHead(200, {"Content-Type": "text/html"});
response.write(file);
response.end();
});
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
This code is simply render one page - gallery/index.html, but render it without images coz can't find them, but how to render directories? for example I want to see index.html in my gallery directory, in apache if I write http://localhost:8888/gallery/
- I will receive index.html page from directory "gallery", how to make the same in node.js?
UPDATE: I solved my problem with node-static module, this answer was very helpful for me https://stackoverflow.com/a/6162856/2560165