2016-10-27 31 views
0

当我使用app.locals时出现undefined。我只是使用app.local作为配置变量。我怎么使用它收听(PORT)内如何在中间件/路由器之外使用app.locals变量

app.js

app.locals = { 
    socket_io_host: 'http://localhost:3001', 
    socket_io_port: "3001", 
}; 

路线/ index.js

var express = require('express'); 
var router = express.Router(); 
var server = require('http').Server(express).listen(app.locals.socket_io_port); 
var io = require('socket.io')(server); 

console.log("["+app.locals.socket_io_port+"]"); 

router.get('/', function(req, res, next) { 
    res.render('index', { title: 'Express',point1: point1,point2: point2 }); 
}); 

回答

0

你必须要求它:

app.js :

module.exports = { 
 
    socket_io_host: 'http://localhost:3001', 
 
    socket_io_port: "3001", 
 
}

index.js:

// put as the first line 
 
var app = require('./app');

相关问题