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

Node.js + app on server not working correctly

Node.js + app on server not working correctly


I'm new to node.js and and tried to connect the server to the client with the example from (no localhost)


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


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


io.sockets.on('connection', function (socket) {
    socket.on('message', function(msg){
        console.log('Got text: '+msg);
    socket.on('disconnect', function () { });


This is the content :)

Google Chrome displays in the console:

Unexpected response code: 502

Also, after receiving every message, Chrome adds

GET http://[myServer]/

to the console. Wheres the problem?

Problem courtesy of: xoxox


The examples from the How-To page all use port 80, which is common for serving websites.

However, you use port 8080 in your example.

Check your web browser's console if it even loads the script. You may need to provide http://localhost:8080/ as explicit url and connect with io.connect('http://localhost:8080');

If the above does not work, please share some insight on what port your web server runs on.

There is no code to actually handle any incoming messages server-side in your (updated) example.

`socket.on('message', function(msg){
  console.log('Got text: '+msg);

should at the very least send the message back to the client - your alert is only triggered when the client receives a message. Does the node.js console output any incoming or sent messages? A few lines of my node.js console look like the following upon connecting.

debug - client authorized
info  - handshake authorized ...
debug - setting request GET /
debug - set heartbeat interval for client ...
debug - client authorized for
debug - websocket writing 1::
debug - sending data ack packet
Solution courtesy of: mabako


View additional discussion.

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

Share the post

Node.js + app on server not working correctly


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription