I am new to node.js. Following some of the tutorials, I created a file named server.js and put this code in that file:
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;
console.log(path);
switch(path){
case '/':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('hello world');
break;
case '/socket.html':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('Inside hello world');
fs.readFile(__dirname + path, function(error, data){
if (error){
response.writeHead(404);
response.write("opps this doesn't exist - 403");
}
else{
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
}
});
break;
default:
response.writeHead(404);
response.write("opps this doesn't exist - 405");
break;
}
response.end();
});
server.listen(8001);
var io.listen(server);
Then I run this using the command: node C:\Users\user\Desktop\server.js and I get this error:
C:\Users\user\Desktop\server.js:38
var io.listen(server);
^
SyntaxError: Unexpected token .
at Module._compile (module.js:439:25)
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:906:3
This line is causing the error:
var io.listen(server);
Initially I thought of installing the npm package socket.io hence I tried this:
npm install --save socket.io
But after this also I am getting the same error:
C:\Users\user\Desktop\server.js:38
var io.listen(server);
^
SyntaxError: Unexpected token .
at Module._compile (module.js:439:25)
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:906:3
Please advice how to resolve this error.
The var keyword is used to declare a variable. I suspect you're missing the variable name:
var foo = io.listen(server);
Other that that, there's no io anywhere else in your code. Are you missing a require call?
The socket.io library is apparently not bundled:
C:\>node
> require("socket.io");
Error: Cannot find module 'socket.io'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at repl:1:1
at REPLServer.self.eval (repl.js:110:21)
at repl.js:249:20
at REPLServer.self.eval (repl.js:122:7)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
Installation though is a one liner:
npm install socket.io
var io.listen(server);
By using the keyword var it expects an assignment statement or declaration, and you're trying to use a method call as a variable name basically.
As for removing it, you'll get a io is not defined error because it looks like you're not calling the module.
var io = require("socket.io");