Reputation: 35
I'm just getting started with node.js, and I'm working through a tutorial. I created a server with the code here:
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
When I run that with node (node server.js) it works. I can got to localhost:8888 and there is a hello world. The next step in the tutorial is creating a server module and then running it from an index.js file. I have copied the tutorial verbatim, and the code is as follows:
index.js:
var server = require("./server");
server.start;
server.js:
var http = require("http");
function start() {
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
Unfortunately node when i try to run the code (node index.js), nothing happens. Going to localhost:8888 gives a can't be reached page. This is from a section in The Node Beginner Book, which I recently purchased. What's going wrong with my code?
Upvotes: 1
Views: 141