Express Generator ignores port setting - node.js

I am using the following code to set the port to 3004 in an express generator app, right above module.exports = app;
// app.js
const PORT = 3004;
app.set('port', PORT);
app.listen(PORT, () => {
console.log(`app listening on port ${PORT}`);
});
I tried using app.set based on this other topic: Node.js/Express.js App Only Works on Port 3000
And app.listen is suggested in the official docs.
They don't work together or in isolation. Running npm start reverts to port 3000, which crashes the app in my case since I'm using that port for another app (also express generator based).
I do not have this issue when starting from my own express app from scratch. Therefore I believe express generator is hiding the port configuration elsewhere and overriding my settings.
Does anyone know where to set the port or disable the overriding setting?
"express": "~4.16.0"

When you use the generator you get a bin folder In your bin folder in www is where the port is set like so:
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
This is where you can change it...

Related

Why does my React app start on my Backend port?

I have a React and NodeJS app. Here is my server/index.js file:
const express = require('express');
const bodyParser = require('body-parser');
require('dotenv').config();
const app = express();
app.use(bodyParser.json());
const items_controller = require('./controllers/items_controller');
app.get('/items', items_controller.getAllItems);
const port = process.env.BACKEND_PORT; // 3333
const server = app.listen( port, () => console.log(`Listening on port: ${port}`) );
When I'm doing npm start it's trying to start React server on my backend server which is 3333 when by default it's 3000. Does anybody know a reason?
There are two possible reasons for this issue:
You might be having something running on PORT 3000(check if any process is using it)
If this is the case, react-scripts will find next available free port (which is probably 3333?) and start the server there.
You might have exported PORT environment variable.
If PORT environment variable is available react-scripts uses it to start the server.

How to get express to listen to external connection?

I am trying to get this little piece of code working in Node:
#!/usr/bin/env node
var debug = require('debug')('server_auth');
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);
});
it is from the Podio API for node:
https://github.com/podio/podio-js/blob/master/examples/server_auth/bin/www
Point is, I get this going from localhost, and can pick it up, but I have no clue how to get Express respond from an external program.
If you in your firewall have that port, in your case port 3000, open you can use any other computer in your network and make the call against your IP:3000 and then the route you want to reach.

NodeJS Deployment confusion between localhost and other domain?

I have a simple program which executes fine in localhost.
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var port = process.env.PORT || 8080;
var host = "127.0.0.1";
var server = app.listen(port, host, function(){
console.log("Server running in : ",host ," with port no : ",port);
});
Trying to deploy the same to heroku using codeship. Everything is building perfect except the last line of deployment test command i.e node index.js which in turn is referring to 127.0.0.1 and stops deploying. May i know do i need to change something here for the host and port address
Just don't provide a host:
var server = app.listen(port, function() {
console.log('Server listening on', port);
});
(This implies, "accept connections on any host, on this port", vs what you're trying which implies, "accept connections on 127.0.0.1 on this port")
Try to run your app on localhost with the help of foreman that is a part of the Heroku Toolbelt. For instance:
foreman start web
You should see your app running on http://localhost:5000 or the port you have specified in your package.json file.
Suggest this link for further queries:
prerequisites to deploy a node app on Heroku?
I was able to host it successfully following through this steps
As suggested by #hunterloftis, i removed hostname.
More importantly, Procfile was missing,so added it and deployed successfully

How to run express.js 4.0 app in webstorm 7

As you might know that new express.js version has came out and it contains most of the changes including restful routes etc. In previous version to run an app we use to set app.js in webstorm but now in express 4.0 to run an app npm is required npm start is command.
Does any body know how to setup an express 4.0 app in webstrom to run from it?
In your Configuration, go to "JavasScript file" and change it to this value: bin\www
Click the 'Run' button (the green triangle), and in the console you should see: "Express server listening on port 3000". Then you can access your app at http://localhost:3000.
I found one hack for this. That is when you create new express.js 4.0 app you will notice that appname/bin/www.txt file get created which contains following code
var debug = require('debug')('my-application');
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);
});
just comment out this line var app = require('../app'); copy that code and paste at bottom of your app.js i.e appname/app.js
that's it now you run an app from node app.js

nodejs app can not run on port 80 using express

I am running the below code taken from express 3.0.3 node_modules/express/test.js:
/**
* Module dependencies.
*/
var express = require('./')
, app = express()
console.log(express.json());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.cookieParser('foobar'));
app.use(express.session());
app.get('/', function(req, res){
res.send('hello');
});
app.listen(80);
console.log('listening on 80');
Now if I start the node app with "sudo node test.js", the app starts fine and I am able to see the 80 port being listened on using netstat, however when I use browser on port 80, then I do not get any response. But I use any port other than 80, it works.
Can you please let me know what I am missing here?
Why I can not run node app on 80 using express?
With regards,
-M-

Resources