Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display html page with node.js

Tags:

node.js

This is my first time with node.js. I get it to display the index.html, but it doesn't display the images on the site or anything else, it ONLY shows the basic html stuff. Here's how I set it up. There's no apache, php or anything else on the server, just ubuntu, proftp and node(and curl and the other dependencies). I made the main directory for the node files /var/nodeFiles and the directory for the html/site files is /var/nodeFiles/www so for my node server file I did it like this:

var http = require('http'),
    fs = require('fs');
fs.readFile('/var/nodeFiles/www/index.html', function (err, html) {
    if (err) {
        throw err; 
    }       
    http.createServer(function(request, response) {  
        response.writeHeader(200, {"Content-Type": "text/html"});  
        response.write(html);  
        response.end();  
    }).listen(80);
});

this works, but it ONLY shows the index.html file and NOTHING attached to it, so no images, no effects or anything that the html file should display. The files and directories are all correct, I've double checked and the permissions of the folders are correct. So what else do I have to do to get node to display the rest of the site? I hope I've explained my self correctly, I was told this is the place to ask development questions. Thank you for taking the time to read this.

like image 594
user2137087 Avatar asked Mar 05 '13 18:03

user2137087


People also ask

How do I load HTML into node js?

var http = require('http'), fs = require('fs'); fs. readFile('./index. html', function (err, html) { if (err) { throw err; } http. createServer(function(request, response) { response.

Can we render HTML file in node JS?

Use the readFile() Method to Render HTML Files in Node. js. We can start by creating a file in our root directory that will contain an HTML file (see the code below). We will name it index.


1 Answers

but it ONLY shows the index.html file and NOTHING attached to it, so no images, no effects or anything that the html file should display.

That's because in your program that's the only thing that you return to the browser regardless of what the request looks like.

You can take a look at a more complete example that will return the correct files for the most common web pages (HTML, JPG, CSS, JS) in here https://gist.github.com/hectorcorrea/2573391

Also, take a look at this blog post that I wrote on how to get started with node. I think it might clarify a few things for you: http://hectorcorrea.com/blog/introduction-to-node-js

like image 100
Hector Correa Avatar answered Oct 02 '22 16:10

Hector Correa