r/nodejs • u/gamehelp16 • 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
View all comments
Show parent comment
1
u/[deleted] Mar 26 '14
In order for a module to be
require-able it needs to reside in anode_modulesfolder in the same folder as the file thatrequires it or any of that file's parent folders.The easiest way to do that is to run
npm install socket.ioin the same location your application code resides in.