I am using Express 4.2.0 and node.js 0.10.12.
The weird thing is that I created a project in C\program files\node\nodetest
and when I did npm start
I got no errors.
Now I created a project in C\program files\node\secondtest
and when I do npm start
I get
app.set('port' , process.env.port 3000) typeerror object #<object> has no method 'set' at object.<anonymous>
and its pointing in C\program files\node\secondtest\bin\www:5:5
Truth is , I dont know how to deal with this error, because I dont get what it means. Is it because both my projects listen on port 3000
?
I just started secondtest
, I installed succesfully the dependencies with npm install
and added this in app.js
var http = require('http');
var express = require('express');
var app = express();
http.createServer(app).listen(3000, function() {
console.log('Express app started');
});
app.get('/', function(req, res) {
res.send('Welcome!');
});
Thanks
EDIT
If I leave the default code in app.js
and www
I get no errors. If I replace the default code of app.js
with mine, and I remove the
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
part from www
, then I get no errors.
Because I guess app.set
and app.get
are depricated in express 4.2.0
? Or because when I set an http server in my app.js
code, conflicts the default www
code? Either one of these, or I am really confused.
EDIT 2
This is the default code of the www
#!/usr/bin/env node
var debug = require('debug')('secondtest');
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
Updated answer according to the updated question.
Since you're calling www
and its code needs to set the port and listen to it, your secondtest
code should not listen to the port. Instead it should export the Express app
as follows:
// ...
module.exports = app;
The www
will do the listening part.
Otherwise, the secondtest
tries to start listening on a port while not exporting the Express app
, and www
tries to listen again on a variable app
which is not an Express app, thus the error object #<object> has no method 'set'
.
When you do var app = require('../app');
in another script, it is important so that this ../app
script actually exports the Express app
.
Old answer.
Do node app.js
instead of using npm
command.
Second, make sure the same port is not used by both processes at the same time. You can't listen to the same port unless you're in cluster mode.
Considering the following is the content of both firsttest
and secondtest
:
var http = require('http');
var express = require('express');
var app = express();
http.createServer(app).listen(process.env.port || 3000, function() {
console.log('Express app started');
});
app.get('/', function(req, res) {
res.send('Welcome!');
});
Do the following to start both apps:
Terminal 1: (the first app will default to port 3000
).
$ node firsttest/app.js
Terminal 1:
$ export PORT=3001
$ node secondtest/app.js