Express.js:找不到文件(即使它在那里)

时间:2016-06-24 17:00:42

标签: javascript node.js express

我正在为我的大学项目构建一个web白板,使用node.js,express.js,validator js,raphael.js,jquery和uglify.js以及socket.io 我刚刚创建了一个服务器脚本,并在命令提示符下使用npm install添加了node_modules。安装了所有必需的库。服务器代​​码

    var
    port = +process.argv[2] || 8080,

    sanitizer = require('validator').sanitize,
    express = require('express'),

    server = express.createServer(),
    io = require('socket.io').listen(server),
    chat = io.of('/chat'),
    canvas = io.of('/canvas')
;

function sanitize(string) {
    return sanitizer(string).entityDecode()
}

server.listen(port);

server.get(/(^\/.*$)/, function(request, response) {
    var fileName = request.params[0];
    if (fileName == '/')
        fileName = '/index.html';
    res.sendFile(__dirname + '/client' + fileName);
});

io.sockets.on('connection', function(socket) {
    socket.on('setName', function (name) {
        name = sanitize(name);
        socket.set('name', name);
        socket.broadcast.emit('receive', {
            sender:'Server',
            message:name + ' has joined.'
        })
    });

    socket.on('send', function (message) {
        socket.get('name', function(error, name) {
            if (name)
                socket.broadcast.emit('receive', {
                    sender:name,
                    message:sanitize(message)
                })
        })
    });

    socket.on('draw', function (command) {
        io.sockets.emit('draw', command)
    });

    socket.on('updateCursor', function(position) {
        socket.get('name', function(error, name) {
            if (name)
                socket.broadcast.emit('updateCursor', {
                    name:name,
                    position:position
                });
        });
    });

    socket.on('disconnect', function() {
        socket.get('name', function(error, name) {
            if (name) {
                socket.broadcast.emit('receive', {
                    sender:'Server',
                    message:name + ' has left.'
                });
                socket.broadcast.emit('removeCursor', name);
            }
        })
    });
});

但是,当我在命令提示符下运行此服务器代码时,我收到此错误

 Error: Cannot find module 'express'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\Users\Akshay\Desktop\whiteboard-master\whiteboard-master\server.js:5:15)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)

现在,我在node_modules文件夹中安装了express.js,代码应该在这里找到。我仍然有同样的错误。我也尝试从那里删除它,并在全局安装express.js.我最近更新到express.js vs 3.4。我仍然得到同样的错误。 有人能帮我吗??我必须在一周的时间内提交项目。

0 个答案:

没有答案