0

我正在尝试让 nowjs 与 express 3.0 一起使用,就像在这个问题中一样。我正在关注代码,但我仍然得到“现在未定义”。

我在 Oracle VM 虚拟机中运行 Ubuntu 12.04。

首先,我创建了一个快速应用程序。

$ express myBrilliantNewApp

然后我在新目录中安装 nowjs。

$ sudo npm install now

添加服务器变量并立即使用它。

var server = http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
})

var everyone = require('now').initialize(server);

那么我应该可以做到这一点......

everyone.now.writesomething = function() {
console.log('I would really like it if this shows up at the console') 
};

...并打电话并检查每个人的所有内容。

now.writesomething();
console.log(everyone);

这没用。我得到:

[ReferenceError: now is not defined]
ReferenceError: now is not defined
at Object.<anonymous> (/home/jan/dev/apps/probeer1/app.js:44:1)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)

谁能告诉我我在这里做错了什么?

完整列表 app.js

/**
* Module dependencies.
*/

var express = require('express')
  , routes = require('./routes')
  , http = require('http')
  , path = require('path');

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', routes.index);

var server = http.createServer(app).listen(app.get('port'), function() {
  console.log('Express server listening on port ' + app.get('port'));
})

var everyone = require('now').initialize(server);


everyone.now.writesomething = function() {
  console.log('I would really like it if this shows up at the console')
};

now.writesomething();
console.log(everyone);
4

2 回答 2

0

在服务器上使用 nowjs 之前,您需要调用:

var nowjs = require('now');

要在客户端上使用 nowjs,您需要在 HTML 中包含脚本引用:

<script src="/nowjs/now.js"></script>
于 2012-08-14T05:15:28.493 回答
0

有关详细信息,请参阅此答案:找不到模块 'now' - nowjs 和 nodejs

npm config set global true && \
     echo 'export NODE_PATH="'$(npm root -g)'"' >> ~/.bashrc && \
     . ~/.bashrc

使用全局标志重试

sudo npm install now -g
于 2012-08-14T22:58:45.367 回答