App.set ('port', process.env.port || 3000) object typeerror # <object> does not have a 'set' method on object. <anonymous>

I am using Express 4.2.0 and node.js 0.10.12.

It is strange that I created the project in C\program files\node\nodetest

, and when I did npm start

, I had no errors.

Now I have 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 toC\program files\node\secondtest\bin\www:5:5

True, I don't know how to deal with this error, because I don't understand what it means. Is it because both of my projects are bugging port 3000

?

I just got started secondtest

, I installed the dependencies successfully with npm install

and added this toapp.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!');
});

      

thank

EDIT

If I leave the default code in app.js

and www

, I get no errors. If I replace the app.js

default code with mine and I remove

app.set('port', process.env.PORT || 3000);

var server = app.listen(app.get('port'), function() {
  debug('Express server listening on port ' + server.address().port);
});

      

out www

, then I get no errors.

Because I think app.set

and app.get

deprived express 4.2.0

? Or is it because when I set the http server in my code app.js

, does the default code conflict www

? Either one of them or I'm really confused.

EDIT 2 This is the default code 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);
});

      

+3


source share


1 answer


Updated answer to match the updated question .

Since you are calling www

and his code must set the port and listen to it, your code secondtest

must not listen on the port. Instead, Express should export app

like this:

// ...
module.exports = app;

      

www

will do the listening part.

Otherwise it secondtest

tries to start listening on the port without exporting Express app

, but www

tries again to listen on a variable app

that is not an Express application, hence the error object #<object> has no method 'set'

.

When you execute var app = require('../app');

in another script, it is important that this ../app

script actually exported Express app

.

Old answer .

Do node app.js

instead of a command npm

.



Secondly, make sure that the same port is not being used by both processes at the same time. You cannot listen on the same port unless you are in cluster mode .

Considering the following: content 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!');
});

      

Follow these steps to launch both applications:

Terminal 1: (the first application will have a port by default 3000

).

$ node firsttest/app.js

      

Terminal 1:

$ export PORT=3001
$ node secondtest/app.js

      

+7


source







All Articles