Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

access is forbidden for node js

access is forbidden for node js

Problem

I am trying to create a simple chat application using Node js. I am using a windows operating system. As local Server I am using Xampp. I have installed node. I have also installed socket.io using package.json. The code in package.json is given below.

{
    "name":"chat",
    "version":"0.0.1",
    "private":"true",
    "dependencies":{
        "socket.io":"0.9.16",
        "express":"3.4.0"
    }
}

Then I have written the code for the server. The node server is running in port 1337. The code for the server is given below.

var io = require('socket.io').listen(1337);

io.sockets.on('connection', function (socket) {
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
        console.log(data);
    });
});

Then when I run it, it is running. Then I have written the code for the client in a index.php file. The code for the client is given below.




    Chat app.

But when I try to the run it with a browser, all I get see in the console is that access is forbidden. BTW, if it is important all my files including node_modules is saved in C:\xampp\htdocs\node. Please help me. I am stuck for quite some days. Thanks in advance.

Problem courtesy of: eddard.stark

Solution

The code you're using is copied from the socket.io Home page and it's only used as an example, but it's not actually working code because the socket.io script isn't being bound to any server instance.

Socket.io isn't a server. It's just a library for nicely handling Websockets. In order to use socket.io you have to require HTTP or Express and create a server instance. Then you'll have to bind the server instance with socket.io.

For a working implementation on how to get socket.io up and running with your server, you'll have to look at the How To Use page. There they have these nice code example, depending on the implementation you're running (if it's HTTP, or something else).

So scratch the whole Xampp server idea. Node has it's own built in server capabilities and that's what you're meant to be using.

Here's a working example (from the socket.io website) of how Socket.io is meant to be used with HTTP. In this code snippet, the server is also created (and it's listening on port 80), so you won't have to worry about that:

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(80);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

Once your server's up and running, you can access it by typing localhost:80 into the browser.

Solution courtesy of: shmuli

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

access is forbidden for node js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×