错误:无法在views目录中查找视图'socket.io'

时间:2016-09-06 17:39:27

标签: javascript node.js express socket.io views

我的nodejs服务器有问题。这是我的代码

server.js

    global.jQuery = global.$ = require('jquery'); 
var  express = require('express'),
    path = require('path'),
    menu = require("./routes/menu");
var sql = require("mssql");
var http = require("http");
var io2 = require("io");
var app = express();


var serve = http.createServer(app);
var io = require('socket.io')(serve);

var recordset2;
var port = 8080;

app.configure(function () {
    app.set('views', __dirname + '/views');
    app.set('view engine','jade');
   /* app.use(express.favicon());*/
    app.use(app.router);
    app.use(express.static(path.join(__dirname+'public')));

});

app.get('/:viewname', menu.viewname);



io.on('connection', function (socket) {
    socket.on('disconnect', function () {
        console.log('user disconnected');
    });
    socket.on('chat', function (msg) {

        socket.broadcast.emit('chat', msg);
    });
});

var dbConfig = {
    server: "localhost",
    database: "MyDatabase",
    user: "sa",
    password: "sa",
    port: 1433
};

function getConnected() {
    var conn = new sql.Connection(dbConfig);
    conn.connect().then(function () {
        var req = new sql.Request(conn);
        req.query("SELECT * FROM Countries").then(function (recordset) {
            console.log("Recordset:", recordset);            
            conn.close();
        }).catch(function (err) {
            console.log("Error!!!!");
            console.log(err);
            conn.close();
        });
    }).catch(function (err) {
        console.log("Error!!!! ----");
        console.log(err);
    });
}

getConnected();

app.listen(port); 

客户代码

var socket = io();
$(function () {
    $('#get-button').on('click', function () {
        console.log("CLICK");
        var msg = "HIIII";
        socket.emit('chat', msg);
    });
});

我正在尝试通过套接字在服务器和客户端之间建立连接,但它返回以下错误:

Error: Failed to lookup view 'socket.io' in views directory C:\Radio/views;
    at Function.app.render (C:\Radio\node_modules\express\lib\application.js:493:17)
    at ServerResponse.res.render (C:\Radio\node_modules\express\lib\response.js:798:7)
    at exports.viewname (C:\Radio\routes\menu.js:2:9)
    at callbacks (C:\Radio\node_modules\express\lib\router\index.js:164:37)
    at param (C:\Radio\node_modules\express\lib\router\index.js:138:11)
    at param (C:\Radio\node_modules\express\lib\router\index.js:135:11)
    at pass (C:\Radio\node_modules\express\lib\router\index.js:145:5)
    at Router._dispatch (C:\Radio\node_modules\express\lib\router\index.js:173:5)
    at Object.router (C:\Radio\node_modules\express\lib\router\index.js:33:10)
    at next (C:\Radio\node_modules\express\node_modules\connect\lib\proto.js:193:15
你可以帮我理解为什么吗?我看到还有其他类似的主题,但没有一个解决方案有帮助。

1 个答案:

答案 0 :(得分:-1)

我在发布此主题后两分钟找到了解决方案。

global.jQuery = global.$ = require('jquery'); 
var  express = require('express'),
path = require('path'),
menu = require("./routes/menu");
var sql = require("mssql");
var http = require("http");
var io2 = require("io");
var app = express();
var port = 8080;
var ser = app.listen(port); //<-----------This solved my problem.