NodeJS Deployment confusion between localhost and other domain? - node.js

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

Related

CRA Socket.io request returns net::ERR_CONNECTION_TIMED_OUT

Hello. I've spent some time without luck trying to understand the problem here.
I've looked through each Question on StackOverflow which seems to deal with the same problem, though nothing has worked so far.
I have a simple chat app built using Create React App and Socket.io (which runs fine on localhost), but when deployed to my Node server I'm receiving ERR_CONNECTION_TIMED_OUT errors and no response. The website itself runs fine, but when I make a call to my Socket.io server, but errors.
I'm guessing this is down to my lack of knowledge with how Node and Socket.io want to work.
Some info:
server.js
const path = require("path");
const express = require("express");
const app = express();
const http = require("http").createServer(app);
const port = 8080;
http.listen(port, () => console.log(`http: Listening on port ${port}`));
const io = require("socket.io")(http, { cookie: false });
app.use(express.static(path.join(__dirname, "build")));
app.get("/*", function (req, res) {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
io.on("connection", (socket) => {
console.log("New client connected");
// Emitting a new message. Will be consumed by the client
socket.on("messages", (data) => {
socket.broadcast.emit("messages", data);
});
//A special namespace "disconnect" for when a client disconnects
socket.on("disconnect", () => console.log("Client disconnected"));
});
client.js
....
const socket =
process.env.NODE_ENV === "development"
? io("http://localhost:4001")
: io("https://my-test-site:8080");
socket.on("messages", (msgs: string[]) => {
setMessages(msgs);
});
....
docker-compose.yml
version: "X.X"
services:
app:
image: "my-docker-image"
build:
context: .
dockerfile: Dockerfile
args:
DEPENDENCY: "my-deps"
ports:
- 8080:8080
Dockerfile
...
RUN yarn build
CMD node server.js // run my server.js
...
UPDATE: I got around this problem by making sure my main port was only used to run Express (with socket.io) - in my set up that was port: 8080. When running in the same Docker container, I don't think I needed to create and use the https version of the express 'createServer'.
This looks like you forgot to map the port of your docker container. The expose statement in your dockerfile will only advertise for other docker containers, which share a docker network with your container, that they can connect to port 4001 of your container.
The port mapping can be configured with the -p flag for docker run commands. In your case the full command look somehow like this:
docker run -p 4001:4001 your_image_name
Also, do you have a signed certificate? Browser will likely block the conneciton if they do not trust your servers certificate.
I got around this problem by keeping just one port available (in my case :8080). This port is what express/socket.io is using (originally I had two different ports, one for my site, one for express). Also, in my case, when running in the same Docker container, I didn't require the require("https").createServer(app) (https) version of the server, as http was sufficient.

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.

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.

Say 'Hello World!' in Openshift with Node.js

I created an app in Openshift and created a local git repo on my computer. I want to change the default welcome page here: http://nodejs-j4nos.rhcloud.com:3000 and just tell Hello world as this tutorial say.
So I removed from local repo the index.html, and modified server.js, pasted in this code below. And commit, and push. I get a long approval, that they accepted my commit.
If I good understand I do not have to stop node and start it again, but Openshift do it for me. But as you can see no Hello World is able to see, when open link in browser (http://nodejs-j4nos.rhcloud.com:3000) why?
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
remote: Git Post-Receive Result: success
remote: Activation status: success
remote: Deployment completed with status: success
To ssh://5556b4c4fcf9336abf0000de#nodejs-j4nos.rhcloud.com/~/git/nodejs.git/
and here is the tree structure, express is listed
Based on this SO answer I tried to modify script, but does not helped:
var express = require('express');
var app = express();
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1');
http.createServer(app).listen(app.get('port'), app.get('ip'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
app.get('/', function (req, res) {
res.send('Hello World!');
});
Link is working now: http://nodejs-j4nos.rhcloud.com The right script to show "Hello world!" is
var http = require('http');
var express = require('express');
var app = express();
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1');
http.createServer(app).listen(app.get('port'), app.get('ip'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
app.get('/', function (req, res) {
res.send('Hello World!');
});
And here is the proof:
Thanks for this: Deployed Node app to OpenShift successfully, OpenShift still shows default page
and this question: Node.js Deployment in openshift
And you should now that you can write in /app-root/repo the $ node server.js command, and if something wrong with script, it will indicate right there
I´ve readed this, maybe you must use a port over 15000:
https://help.openshift.com/hc/en-us/articles/202185874-I-can-t-bind-to-a-port
Found my notes on using OpenShift with Node:
The openshift system has some integrated control tools to support its ‘gear’ system, useful to control the openshift
application and environment.
gear control your application (start, stop, restart, etc)
or deps with --cart (gear start --cart mysql-5.1)
tail_all tail all log files. This command displays the last entries
in your log files as they are written. You can hit
<ctrl>-c to exit this command.
export list available environment variables
rm remove files / directories
ls list files / directories
ps list running applications
kill kill running applications
mysql interactive MySQL shell
mongo interactive MongoDB shell
psql interactive PostgreSQL shell
quota list disk usage
The gear system has additional commands. OpenShift Gear Control, An assortment of gear utilities:
COMMANDS:
build Run the build steps
deploy Run the deploy steps
help Display global or [command] help documentation.
postreceive Run the git postreceive steps
prereceive Run the git prereceive steps
reload Reload a cart
remotedeploy Run the remotedeploy steps
restart Restart a cart
restore Restore an application
snapshot Snapshot an application
start Start the gear/cart
status Get the status for a cart
stop Stop the gear/cart
Will any of this stuff help you stop and restart the gear? I'd start with the simple 'gear' command. I don't remember, is Express loaded up via NPM or now native with node? At one time it was a NPM install. Those don't get pushed to Openshift.
What is the directory tree structure on the openshift nodejs server?
root
\ app-root
\ data
\ repo <- - the working files for web content end up here.
\ runtime
\ git
\ nodejs
In openshift dependencies don't get pushed. For that you can login thru ssh and go to:
cd app-root/repo or cd $OPENSHIFT_REPO_DIR and then npm install tool_of_choice

Resources