user995487
user995487

Reputation: 141

Modules in Node.js

I am trying to understand how to use modules in node.js to organize my code. Here are my two files 1. server.js 2. index.js

server.js

var http = require("http");

function onRequest(request, response) {
    console.log("Request received.");
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
}

exports.start = function () {
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}

index.js

var server = require('./server.js').inspect;

server.start();

But when i execute

node index.js

I get this following error.

ashwin@ashwin-vm:~/winshare/node-tut1$ node index.js

/home/ashwin/winshare/node-tut1/index.js:3
server.start();
   ^
TypeError: Cannot call method 'start' of undefined
at Object.<anonymous> (/home/ashwin/winshare/node-tut1/index.js:3: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:902:3
ashwin@ashwin-vm:~/winshare/node-tut1$ 

Here are my node and Ubuntu versions

ashwin@ashwin-vm:~/winshare/node-tut1$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 13.10
Release:    13.10
Codename:   saucy
ashwin@ashwin-vm:~/winshare/node-tut1$ 
ashwin@ashwin-vm:~/winshare/node-tut1$ node --version
v0.10.25

Upvotes: 0

Views: 88

Answers (1)

Paul
Paul

Reputation: 141935

Remove the .inspect from this line:

var server = require('./server.js').inspect;

Upvotes: 4

Related Questions