19

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.

4
  • Total duplicate of stackoverflow.com/questions/6126584/… Commented Mar 5, 2013 at 18:59
  • Basically what you want to do is serve static content There have been a lot of questions about this posted before. Commented Mar 5, 2013 at 19:01
  • 1
    he's generating the html in the server file. I was trying to take an existing site and display it using the fs library. If re-writing it that way is what i must do, so be it. Commented Mar 5, 2013 at 19:04
  • Use full file/image url instead of relative url. Commented Jun 1, 2017 at 7:10

5 Answers 5

24

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

Sign up to request clarification or add additional context in comments.

3 Comments

Your blog has been very helpful. Coming from PHP to node, there's a few concepts that should be cleared-up. I have a few people I just sent your blog to that have been having a bit of a time making the convert.
how would i use this on AWS if i dont have a static ip? var serverUrl = "127.0.0.1"; and var serverUrl = "http://ec2-xx-xx-xx-xx.compute-1.amazonaws.com/"; does not work
fixed by commenting out: }).listen(port/* , serverUrl */);
11

Check this basic code to setup html server. its work for me.

var http = require('http'),
    fs = require('fs');


fs.readFile('./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(8000);
});

2 Comments

this only displays index.html and no other files in that directory
In case if i don't know the path, i only know the url that too of this kind "www.example.com/example/example1"
9

This did the trick for me:

var express = require('express'),
app = express(); 
app.use('/', express.static(__dirname + '/'));
app.listen(8080);

Comments

1

If your goal is to simply display some static files you can use the Connect package. I have had some success (I'm still pretty new to NodeJS myself), using it and the twitter bootstrap API in combination.

at the command line

:\> cd <path you wish your server to reside>
:\> npm install connect

Then in a file (I named) Server.js

var connect = require('connect'),
   http = require('http');
connect()
   .use(connect.static('<pathyouwishtoserve>'))
   .use(connect.directory('<pathyouwishtoserve>'))
   .listen(8080);

Finally

:\>node Server.js

Caveats:

If you don't want to display the directory contents, exclude the .use(connect.directory line.

So I created a folder called "server" placed index.html in the folder and the bootstrap API in the same folder. Then when you access the computers IP:8080 it's automagically going to use the index.html file.

If you want to use port 80 (so just going to http://, and you don't have to type in :8080 or some other port). you'll need to start node with sudo, I'm not sure of the security implications but if you're just using it for an internal network, I don't personally think it's a big deal. Exposing to the outside world is another story.

Update 1/28/2014:

I haven't had to do the following on my latest versions of things, so try it out like above first, if it doesn't work (and you read the errors complaining it can't find nodejs), go ahead and possibly try the below.

End Update

Additionally when running in ubuntu I ran into a problem using nodejs as the name (with NPM), if you're having this problem, I recommend using an alias or something to "rename" nodejs to node.

Commands I used (for better or worse):

Create a new file called node

:\>gedit /usr/local/bin/node
#!/bin/bash
exec /nodejs "$@"

sudo chmod -x /usr/local/bin/node

That ought to make

node Server.js 

work just fine

3 Comments

I get an error in my browser that says Cannot GET /. Ideas?
Also when I set static to '../' I get an error saying that undefined is not a function.
I suspect this is due to updates in the packages, the simple example stopped working for me after connect updates.... I'd have to do some digging to see what the latest version uses (I've found it at one point but don't see any examples on github right now).*Long story short, what version of the tools are you using, that will drive an answer.
0

You can simply use

res.senFile('PATH_TO_FILE');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.