我在未传递到javascript文件的玉模板文件(index.jade)中声明的变量(config)遇到问题,这会使我的javascript崩溃。这是文件(views / index.jade):
h1 #{title}
script(src='./socket.io/socket.io.js')
script(type='text/javascript')
var config = {};
config.address = '#{address}';
config.port = '#{port}';
script(src='./javascripts/app.js')
这是我的app.js(服务器端)的一部分:
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.set('address', 'localhost');
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', function(req, res){
res.render('index', {
address: app.settings.address,
port: app.settings.port
});
});
if (!module.parent) {
app.listen(app.settings.port);
console.log("Server listening on port %d",
app.settings.port);
}
// Start my Socket.io app and pass in the socket
require('./socketapp').start(io.listen(app));
这是我的javascript文件崩溃的一部分(public / javascripts / app.js):
(function() {
var socket = new io.Socket(config.address, {port: config.port, rememberTransport: false});
我在本地主机(我自己的机器)上的开发模式(NODE_ENV = development)上运行站点。我正在使用node-inspector进行调试,它告诉我config变量在public / javascripts / app.js中未定义。
有任何想法吗??谢谢!!