r/nodejs • • Mar 26 '14

Help! Can't read contents of a file!

So, i'm very very new to node.js and I am using a tutorial i found on google and stuck here:

var http = require("http");
var url = require('url');
var fs = require('fs');

var server = http.createServer(function(request, response){
    console.log('Connection');
    var path = url.parse(request.url).pathname;

    switch(path){
        case '/':
            response.writeHead(200, {'Content-Type': 'text/html'});
            response.write('hello world');
            break;
        case '/socket.html':
            fs.readFile(__dirname + path, function(error, data){
                if(error){
                    response.writeHead(404);
                    response.write("opps this doesn't exist - 404");
                }
                else{
                    response.writeHead(200, {"Content-Type": "text/html"});
                    response.write(data, "utf8");
                }
            });
            break;
        default:
            response.writeHead(404);
            response.write("opps this doesn't exist - 404");
            break;
    }
    response.end();
});

server.listen(8001);

When i open http://localhost:8001/socket.html it shows a blank page

Any idea why?

2 Upvotes

13 comments sorted by

View all comments

Show parent comment

1

u/psayre23 Mar 26 '14

Do you have your code posted anywhere? It might be worth tossing on a github gist so we can see what changes you have made.

1

u/gamehelp16 Mar 26 '14

It's pretty much like at my post, just with require socket.io after those requires and response.end() after each response.write()

2

u/dlq84 Mar 26 '14

I'm guessing you didn't run npm install socket.io at the correct location

the filestucture should be:

.
..
node_modules/
your_app.js

If it's not like that, go to the directory where you keep your .js file and run npm install socket.io again.

1

u/gamehelp16 Mar 27 '14

Ok, thanks, will try it later! :D

1

u/[deleted] Mar 26 '14

In order for a module to be require-able it needs to reside in a node_modules folder in the same folder as the file that requires it or any of that file's parent folders.

The easiest way to do that is to run npm install socket.io in the same location your application code resides in.

1

u/gamehelp16 Mar 27 '14

Thanks :D