-1

I am trying to start a HTTP server from an index.js page. I have been reading The Node Beginner and following the tutorial there but each time i try starting a HTTP server, i get an error. This is the code

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(10000);
      console.log("Server has started.");
   }

and then, index.js -

   var server = require("./server");
   server.start();

But each time i try running index.js, i get this error -

 server.start();
       ^
    TypeError: Object #<Object> has no method 'start'
      at Object.<anonymous> (C:\wamp\www\nodeStack\index.js:2:8)
      at Module._compile (module.js:456:26)
      at Object.Module._extensions..js (module.js:474:10)
      at Module.load (module.js:356:32)
      at Function.Module._load (module.js:312:12)
      at Function.Module.runMain (module.js:497:10)
      at startup (node.js:119:16)
      at node.js:901:3
   Program exited.

Please how do i resolve this, thanks.

gbade_
  • 339
  • 1
  • 8
  • 21

4 Answers4

2
 var http = require("http");

  server.start = function () {
     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(10000);
     console.log("Server has started.");
   };
      exports.start = start;
StriderWaR
  • 23
  • 6
1

You probably oversaw the last line from the example code on http://www.nodebeginner.org for server.js. This last line reads

exports.start = start;

If you add it to the end of the server.js file, all should be fine.

Manuel Kießling
  • 656
  • 6
  • 10
0

You must change your server.js to export function start. You can also read very good explanation about module.exports here: What is the purpose of Node.js module.exports and how do you use it?

var http = require("http");
var server = module.exports = {};

server.start = function () {
    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(10000);
    console.log("Server has started.");
};
Community
  • 1
  • 1
Andrew Surzhynskyi
  • 2,726
  • 1
  • 22
  • 32
  • I read on exporting modules and also followed your answer, but now i'm getting this error - events.js:72 throw er; // Unhandled 'error' event. Error: listen EADDRINUSE. And prior to this aspect of the tutorial, i had used this same port and it worked – gbade_ Feb 26 '14 at 10:27
  • EADDRINUSE means that the port number which listen() tries to bind the server to is already in use. Just change the port number 10000 to 3000 or 3001 or any other till error will gone. Also look if another copy of your application is already running, you cannot start two node instances listening on same port. – Andrew Surzhynskyi Feb 26 '14 at 10:31
0

I just ran into this exact problem. The fix for me was to remove the () when calling server.start in the index.js

Try:

var server = require("./server");
server.start;