Reputation: 51
I`m trying to get nowjs to work with express 3.0 just like in this question. I am following the code but I still get "Now is not defined".
I am running Ubuntu 12.04 in an Oracle VM virtual box.
First I create an express app.
$ express myBrilliantNewApp
Then I install nowjs in the new dir.
$ sudo npm install now
Add the server variable and use it with 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);
Then I should be able to do this...
everyone.now.writesomething = function() {
console.log('I would really like it if this shows up at the console')
};
...and make the call and check what everyone is all about.
now.writesomething();
console.log(everyone);
It does not work. I get:
[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)
Can anyone tell me what I am doing wrong here?
Full listing 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);
Upvotes: 0
Views: 1199
Reputation:
See this answer for details: Cannot find module 'now' - nowjs and nodejs
npm config set global true && \
echo 'export NODE_PATH="'$(npm root -g)'"' >> ~/.bashrc && \
. ~/.bashrc
Try again with global flag
sudo npm install now -g
Upvotes: 0
Reputation: 312129
Before using nowjs on the server you need to call:
var nowjs = require('now');
To use nowjs on the client you need to include a script reference in your HTML:
<script src="/nowjs/now.js"></script>
Upvotes: 0