nodejs app can not run on port 80 using express - node.js

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-

Related

Output in browser using Express Nodejs

I install nodejs with express in server (subdirectory in ftp,path is /var/www/html/admin)
and in admin folder i created file "app.js" which is working in xshell fine and showing message in console
here is my app.js
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
but now i want to show result in browser,how can i do this
i tried with following urls but not working for me
http://myurl.com/admin:3000
http://myurl.com/3000/admin
First get ip address of your FTP server.
Then connect http://<FTP_SERVER_URL:3000 to see 'Hello World' you wrote in the code.
To browse /admin folder you mentioned and if your express app is running on root directory, try http://<FTP_SERVER_URL:3000/var/www/html/admin
You can use document.write() method to write something to the DOM object( or in layman's language you can call it browser screen).
Try using the following:
app.listen(port, () => document.write(`Example app listening on port ${port}!`))

Express Generator ignores port setting

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...

Can't deploy Node restify app to AWS EB

I'm trying to deploy a basic restify node app to AWS EB but when I do I get a 502 Bad Gateway error. The AWS console also shows that the application is in Health: Severe. It seems as though it isn't correctly serving via port 80. Here is my server.js:
var restify = require('restify');
var server = restify.createServer();
server.get('/', function(req, res) {
res.send("Hello");
return next();
});
var port = process.env.PORT || 3000;
server.listen(port);
Seems as though on EB that nginx redirects to node on port 8081, so I changed to that.
Additionally for whatever reason the node instance wasn't being run after "eb deploy". I added "node server.js" in software configuration on aws and now it seems to work fine.

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

Hosting a nodejs express site on Windows Azure

I created a new NodeJS site based on the documentation of Durandal. It works locally. I set up Windows Azure to pull in the changes of my GitHub repository, which it does correctly.
However, (after enabling errors) I'm getting the an internal server error.
I checked my FTP for the logs, but don't have any. There is a 'Git' folder, but nothing interesting there.
When I change my server.js file to the hello world sample, everything works:
var http = require('http')
var port = process.env.PORT || 1337;
http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(port);
This is my server.js now:
var express = require('express'),
routes = require('./routes'),
engines = require('consolidate');
exports.startServer = function(config, callback) {
var port = process.env.PORT || config.server.port || 1337;
var app = express();
var server = app.listen(port, function() {
console.log("Express server listening on port %d in %s mode", server.address().port, app.settings.env);
});
app.configure(function() {
app.set('port', port);
app.set('views', config.server.views.path);
app.engine(config.server.views.extension, engines[config.server.views.compileWith]);
app.set('view engine', config.server.views.extension);
app.use(express.favicon());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.compress());
app.use(config.server.base, app.router);
app.use(express.static(config.watch.compiledDir));
});
app.configure('development', function() {
app.use(express.errorHandler());
});
app.get('/', routes.index(config));
callback(server);
};
So, has anyone succesfully hosted an Express (or more specifically, the Durandal skeleton for Mimosa) NodeJS site on Windows Azure? Or do you know how or where I can find the internal server error?
I've launched a few express applications on the Windows Azure platform and found that I had several instances of it failing quite silently. I personally have found the approach suggested in this post by Jay Harris really helpful as it allows me to import dependencies (npm, bower or other) and run grunt tasks to compile the project etc.
A few things worth noting is that often after a new deploy the updates did not display until restarting the Azure server in the control panel. Sometimes the deploy scripts timed out and I had to check them regularly.
This doesn't exactly answer what's wrong with your code (sorry) but I've posted an example using the method mentioned above that may help. The main deploy files are 'web.config', 'deploy.sh' and '.deployment' as well as your 'package.json' file.

Resources