如何在中间件/路由器之外使用app.locals变量

时间:2016-10-27 01:15:28

标签: node.js express

当我使用app.locals时,我得到了未定义。我只是使用app.local作为配置变量。我如何在listen(PORT)中使用它

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 });
});

1 个答案:

答案 0 :(得分: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');