NodeJS app crashing on Heroku with error "Stopping process with SIGKILL" - node.js

The code shown below fails on Heroku with the error Stopping process with SIGKILL
http.createServer((req, res) => {
var server_port = process.env.YOUR_PORT || process.env.PORT || 80;
var server_host = process.env.YOUR_HOST || '0.0.0.0';
server.listen(server_port, server_host, function() {
console.log('Listening on port %d', server_port);
});
});
Logs:
heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
How can I fix this issue?

You have not declared the variable server.
You can do:
var server = http.createServer((req, res) => {
//your stuff
});
server.listen(process.env.PORT || 80, () => {
console.log("Listening on port 80");
});

Related

How to fix Node express error: cannot GET

I'm trying to create a simple backend api for an app I'm working on (create-react-app) and decided to use node express. I'm getting the error 'cannot GET' when I open my browser and I don't understand why.
Here is my server/index.js file:
const express = require("express");
const PORT = process.env.PORT || 3001;
const app = express();
app.get("/api", (req, res) => {
res.json({ message: "Hello from server!" });
});
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});
Here is my start script:
"scripts": {
"start": "node index.js"
}
In my console I see this error:
Failed to load resource: the server responded with a status of 404 (Not Found)
This is my folder structure:
I have an app called 1REPMAX that contains the folders
>build
>node-modules
>public
>server
>node_modules
>index.js
>package-lock.json
>package.json
>src
.gitignore
package-locj.json
package.json
So what I've been doing is I cd into server and then run npm start.
When I open my browser to localhost:3001 I get the error. Any help on what I'm doing wrong? If more information is needed just let me know. Also, when I ran npm start the first time it started up fine, but now every time I run it I get this error:
code: 'EADDRINUSE',
errno: -48,
syscall: 'listen',
address: '::',
port: 3001
I don't know if this has something to do with it or not.
The first error is caused because your app is not listening to the / path, only the /api path.
The second error is caused by a duplicate app.listen() call in your server/index.js file.
Your code should look something like this in order to resolve the issues:
const express = require("express");
const PORT = process.env.PORT || 3001;
const app = express();
app.get("/", (req, res) => res.send("Main page"));
app.get("/api", (req, res) => {
res.json({ message: "Hello from server!" });
});
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});

Why heroku fails to bind to $PORT when I use webpack.definePlugin with parsed dotenv

I am trying to deploy a node.js app to heroku which is bundled with webpack. Due to heroku logs I face with the following error:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
App does not use a fixed port to listen on.
app.listen(process.env.PORT || 3000, () => {
log(`App is running: 🌎 http://localhost:${process.env.PORT || 3000}`, 'info');
});
I have also tried to use '0.0.0.0':
app.listen(process.env.PORT || 3000, '0.0.0.0' () => {
log(`App is running: 🌎 http://localhost:${process.env.PORT || 3000}`, 'info');
});
I noticed that if I use webpack.definePlugin with parsed dotenv I face with this issue, but if I don't use it all is fine.
I have the following .env file that I use for webpack.definePlugin:
const fs = require('fs');
const paths = require('./paths');
const defaultVars = {
NODE_ENV: process.env.NODE_ENV || 'development'
};
const stringifyEnv = (obj) => ({
'process.env': Object.keys(obj).reduce((env, key) => {
env[key] = JSON.stringify(obj[key]);
return env;
}, {})
});
module.exports = () => {
if (fs.existsSync(paths.dotenv)) {
const dotenv = require('dotenv').config();
const env = { ...dotenv.parsed, ...defaultVars };
return stringifyEnv(env);
}
return stringifyEnv(defaultVars);
};
Tried to list dotenv in devDependencies.
Could you please tell what can be wrong?
I managed to make it working by passing a port explicitly.
Procfile
web: node build/server/server.js --port $PORT
server.js
const argv = require('yargs').argv;
...
app.listen(argv.port || process.env.PORT || 3000, () => {
log(`App is running: 🌎 http://localhost:${argv.port || process.env.PORT || 3000}`, 'info');
});
But it still does not answer the question above.

Https server with socket and express

I'm using SSL to encrypt my backend but my current solution opens two ports, one for sockets and the other for express, any approach to start both on the same port like HTTP ?
const port=4000;
if(process.env.ENABLE_SSL=='true')
{
////two ports are open 8989,4000
server = https.createServer({
key: fs.readFileSync("sslLocation/ssl.key"),
cert: fs.readFileSync("sslLocation/ssl.cert")
},app).listen("8989", '0.0.0.0',function(){
console.log('Express server listening to port 8989');
});
global.io = require('socket.io').listen(server);
app.listen(port);
}
else
{
////one port only
// start the server
server = app.listen(port, function () {
console.log(`App is running at: localhost:${server.address().port}`);
});
global.io = require('socket.io').listen(server);
}
also running app.listen(server) in the ssl section, i can't access the apis

Swagger running on Heroku: port problems?

I am using nodeJS, express, swagger-tools to get Swagger API document showing from /docs in the my Heroku dyno. Locally this works, but on Heroku the port is not listening.
app[web.1]: info: Swagger-ui is available on localhost:5765/docs
heroku[web.1]: State changed from crashed to starting
heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
heroku[web.1]: Stopping process with SIGKILL
heroku[web.1]: Process exited with status 137
On the code, I have the port predetermined by Heroku.
var listen_addr = 'localhost';
var listen_port = process.env.PORT || 8080;
var swaggerDoc = require(options.swaggerUi);
swaggerDoc.host = listen_addr +":" + listen_port; // making sure of add
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
app.use(middleware.swaggerMetadata());
app.use(middleware.swaggerValidator());
app.use(middleware.swaggerUi() );
app.use(middleware.swaggerRouter(options) );
app.listen(listen_port, listen_addr) {
winston.info('Server is listening on %s:%d', listen, port);
winston.info('Swagger-ui is available on %s:%d/docs', listen, port);
});
});
UPDATE: the problem seemed to be solved by changing app.listen to app.listen( process.env.PORT || 3000, function()

Openshift port for node.js

I have a server that won't start correctly on openshift. This is my code:
var connect = require("connect");
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var httpServer = connect.createServer(connect.static(__dirname + "/public")).listen(port);
console.log("Listening on " + port + "...");
I keep getting this error:
info: socket.io started
warn: error raised: Error: listen EACCES
DEBUG: Program node server.js exited with code 0
DEBUG: Starting child process with 'node server.js'
Listening on 8080...
How can I solve this?
You need to bind the listening IP address to process.env.OPENSHIFT_NODEJS_IP. Here is the example from my working code (I'm using Express) on OpenShift.
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
app.listen(port, ipaddress, function() {
// Do your stuff
});
We need to specify the binding to your OPENSHIFT_NODEJS_IP in your listen like:
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1"; (do not forget quotes)
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;

Resources