not being served by Node.js server

As I understood it, from
, node.js
automatically serves the file on the server.

I have installed with npm install
and I can see that it resides in node_modules
one level above the server root.


var static = require('./plugins/node-static');
var socketIO = require('');
var clientFiles = new static.Server('./client');

var http = require('http');
httpServer = http.createServer(function (request, response) {
    request.addListener('end', function () {
            clientFiles.serve(request, response);

var webSocket = socketIO.listen(httpServer);
webSocket.on('connection', function(client) { .....




        $(document).ready(function() {
            var webSocket = new io.Socket('localhost', { port: 8253 });
            webSocket.connect(); .......

Starting the server works fine, but when opening index.html, I receive the following error:

GET http://localhost:8253/ 404 (Not Found)
Uncaught ReferenceError: io is not defined                 :8253/:25


Problem courtesy of: Soroush Hakami


Try listening on the server after you bind it with

Place this



var webSocket = socketIO.listen(httpServer);

Solution courtesy of: fent


Edited: Apologies, I have written something that did not answer your question.

On the client side you need the following:

var socket = io.connect(); //Hostname and port not required - Autodetected
socket.on('connect', function(){
socket.on('message', function(m){
socket.on('disconnect', function(){

Working example =>

NPM Information (if required): If you are in Linux

npm install -g socket.ion #install globally
npm link #Create a symbolic link

If you are on Windows you can’t do npm link

npm install

Your directory structure should look like

node_modules/ #Directory - same level as server.js underneath that

node_modules should be in the same directory as server.js, not above server root

Discussion courtesy of: First Zero

For those deploying on Azure (I can’t vouch for any other platforms) make sure that your package.json file includes a start script.


"scripts" : { "start": "node index.js" }

Discussion courtesy of: Bryan Gordley

This recipe can be found in it’s original form on Stack Over Flow

稿源:Node.js Recipes (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » not being served by Node.js server

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录