How to fix express.js error in Openshift? - node.js

I've installed express.js in specificed folder scoreboard, I'm trying to execute "npm start" in CLI, then I got error was:
0 info it worked if it ends with ok
1 verbose cli [ 'node', '/opt/rh/nodejs010/root/usr/bin/npm', 'start' ]
2 info using npm#1.3.24
3 info using node#v0.10.25
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info prestart application-name#0.0.1
6 info start application-name#0.0.1
7 verbose unsafe-perm in lifecycle true
8 info application-name#0.0.1 Failed to exec start script
9 error application-name#0.0.1 start: `node app.js`
9 error Exit status 8
10 error Failed at the application-name#0.0.1 start script.
10 error This is most likely a problem with the application-name package,
10 error not with npm itself.
10 error Tell the author that this fails on your system:
10 error node app.js
10 error You can get their info via:
10 error npm owner ls application-name
10 error There is likely additional logging output above.
11 error System Linux 2.6.32-504.3.3.el6.x86_64
12 error command "node" "/opt/rh/nodejs010/root/usr/bin/npm" "start"
13 error cwd /var/lib/openshift/54d99b5f5973ca0a11000120/app-root/runtime/repo/Scoreboard/scoreboard
14 error node -v v0.10.25
15 error npm -v 1.3.24
16 error code ELIFECYCLE
17 verbose exit [ 1, true ]
There content of app.js is:
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
// all environments
//app.set('port', process.env.PORT || 3000);
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('ipaddr', process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1");
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
I had no idea how to fix it, I'm looking a solution how to run express in Openshift, Thanks!

I know it's been a long time since this issue was opened, but I could deploy it successfully to Openshift.
Like Thimoty Gu said, there were some problems in your app.js. because some features you are using from Express are deprecated, what can impact to deploy an app to Openshift.
In Openshift I've had problems with very simple things, like setting the package.json with * instead of the right version of the dependency I wanted.
I did a few changes in your code without the routes file and it worked in my test. Basically, the result is:
package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies" : {
"express" : "4.9.5" //it's important to set a version, instead of using *
}
}
server.js
/**
* Module dependencies.
*/
var express = require('express')
//, routes = require('./routes')
//, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
// all environments
//app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
//app.use(express.favicon());
//app.use(express.logger('dev'));
//app.use(express.bodyParser());
//app.use(express.methodOverride());
//app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
//if ('development' == app.get('env')) {
// app.use(express.errorHandler());
//}
app.get('/', function(req, res, err) {
res.send("You have accessed page / (root)");
});
app.get('/users', function(req, res, err) {
res.send("You have accessed page /users (users)");
});
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var address = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
console.log("Port: " + port);
console.log("Address: " + address);
var server = app.listen(port, address, function() {
console.log('Express server listening on port ' + port);
});
In summary, I just removed/commented your libraries that are not supported or are deprecated in Express. To use them now, you should include them from the new libraries that support them.
Hope it's somehow useful, even so long later.

Related

AgoraIO Web + Heroku

I'm trying to push github repo LargeGroupVideoChat-Web-Webpack to Heroku.
Locally it works fine, with these scripts in package.json file:
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --config ./scripts --mode development",
"build": "cross-env NODE_ENV=production webpack --config ./scripts --mode production"},
And webpack settings in index.js file:
module.exports = {
entry: {
index: "./src/index.js",
},
devtool: "inline-source-map",
module: loaders,
plugins,
resolve: {
extensions: [ ".js" ],
},
output: {
filename: "[name].[hash].js",
path: path.resolve(__dirname, distPath),
}, ...
However, after pushing to Heroku it crashes with error. I added server.js file with this code:
const express = require('express');
const path = require('path');
const port = process.env.PORT || 8080;
const app = express();
// the __dirname is the current directory from where the script is running
app.use(express.static(__dirname));
// send the user to index html page inspite of the url
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'index.html'));
});
app.listen(port);
It runs ok, but it only serves index.html file, and webpack bundle is not linked. Is there a fast way to push the app to Heroku? Or I have to rewrite webpack settings somehow?
Ok, the server.js file solved the problem (from another repo):
var path = require('path');
var express = require('express');
var app = express();
app.use(express.static(path.join(__dirname, 'dist')));
app.set('port', process.env.PORT || 8080);
var server = app.listen(app.get('port'), function() {
console.log('listening on port ', server.address().port);
});

Failed at the app#1.0.0 start script This is probably not a problem with npm. There is likely additional logging output above

Hi Guys, I have created a little project of Mern stack. I am deploying it correctly on Heroku. But as soon as I am checking her on Heroku after deploying, then Failed to load resource: the server responded with a status of 503 (Service Unavailable) error is coming.
But as soon as I run heroku local in cmd after deploying then it is working correctly. I am giving below the heroku setup code. Please guide me. please ........
package.sjon file of backend
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "npm install && node index",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"dependencies": {
"config": "^3.3.1",
"express": "~4.16.1",
"express-fileupload": "^1.1.7-alpha.3",
"mongoose": "^5.9.12",
"nodemailer": "^6.4.6"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Index.js file of backend
var express = require('express');
var path = require('path');
const connectDB = require('./config/db')
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var studentRouter = require('./routes/student')
var app = express();
connectDB();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static('client/build'))
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
})
app.use('/', indexRouter);
app.use('/', studentRouter);
app.use('/users', usersRouter);
if (process.env.NODE_ENV == "production") {
app.use(express.static('client/build'))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
})
}
var port = process.env.PORT || '8000';
app.listen(port, () => {
console.log(`server run port ${port}`);
})
module.exports = app;
All these codes are absolutely correct. I had a problem creating a cluster in mongodb atlas. The problem was that I had selected add current ip address while creating the cluster. Whereas I had to select allow from anywhere. So now I have selected the book. And now it is doing the right thing.
In my case, changing network access form anywhere in my MongoDB cluster, fixed the problem.
Also, don't forget to hit restart all dynos.

Nodejs Heroku Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

I have tried number of "Error R10" Solutions, but it doesn't solve the problem for a simple Hello World like Nodejs app with server.js, package.json, index.html and node_module
Hereby adding server.js, package.json and error log
Currently trying this app on free heroku acount
Is it necessary to upload nodejs app on github to host it on heroku
server.js
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var port = 3000;
var app = express();
// Set Static Folder
app.use(express.static(__dirname));
// Body Parser MW
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
var env = process.env.NODE_ENV || 'development';
if ('development' == env) {
app.use(express.static(__dirname + '/'));
} else {
app.use(express.static(__dirname + '/'));
}
app.get("/", function(req, res){
res.render("index.html");
});
app.listen(port, function(){
console.log("Server started ..!");
});
package.json
{
"name": "es1",
"version": "1.0.0",
"description": "eS",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.16.0",
"express": "^4.14.0"
}
}
Attaching heroku logs:
https://drive.google.com/open?id=0B4XtAe7mRM6UVllTQWh1dmplZk0
Change the value of port as follows:
var port = process.env.PORT || 3000;

Error Deploying node js app to AWS Elastic Beanstalk

I deployed node js application to AWS EBS. When I run the app, i get the error " 502 Bad Gateway " nginx/1.6.2 . This is what i found out in the log.
env.elasticbeanstalk.com/"
2015/04/21 10:52:01 [error] 3807#0: *86 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.4.86, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "clinicaltrials-env.elasticbeanstalk.com"
This is my package.json file
{
"name": "Clinical_Trial_Analytics",
"version": "1.3.2",
"private": true,
"scripts": {
"start": "node main.js"
},
"dependencies": {
"express": "3.0.6",
"stylus": "0.31.0",
"jade": "0.27.7",
"mongodb": "1.2.7",
"moment" : "1.7.2",
"emailjs": "0.3.3",
"json2html": "1.0.0"
},
"engines": {
"node": "0.12.0",
"npm": "1.1.65"
}
}
This is my main.js file
var express = require('express');
var http = require('http');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 8080);
app.set('views', __dirname + '/server/views');
app.set('view engine', 'jade');
app.locals.pretty = true;
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: 'super-duper-secret-secret' }));
app.use(express.methodOverride());
app.use(require('stylus').middleware({ src: __dirname + '/client' }));
app.use(express.static(__dirname + '/client'));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
require('./server/router')(app);
http.createServer(app).listen(app.get('port'), function(){
console.log("Clinical Trials server listening on port " + app.get('port'));
})
I am unable to figure out the issue. Is it with the node version or any other problem? Thanks in advance.
It's important to add the Node command at Configuration Modify software section of your beanstalk app, I can see in your app you are using the command start, so use it as node command, "npm start" that will start your app correctly, this also happens when use the folder and file bin/www to start the Nodejs server.

nodeJS : throw new Error('Can\'t set headers after they are sent.');

This is common SO question but didn't get the solution. So I am again putting it here.
Here is app.js
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
var server = http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
require('./routes/sockets.js').initialize(server);
here is sockets.js inside routes folder
var io = require('socket.io');
exports.initialize = function(server) {console.log('init called');
io = io.listen(server);console.log('io');
io.sockets.on("connection", function(socket){console.log("connected");
socket.send(JSON.stringify({type:'serverMessage',message: 'Welcome to the most interesting chat room on earth!'}));
socket.on('message', function(message){
message= JSON.parse(message);
if(message.type == "userMessage")
{
socket.broadcast.send(JSON.stringify(message));
message.type = "myMessage";
socket.send(JSON.stringify(message));
}
});
});
};
Inside sockets.js console.log('init called'); console.log('io'); is printing well. When i run this app using npm start is got following error:
npm WARN package.json application-name#0.0.1 No repository field.
npm WARN package.json application-name#0.0.1 No readme data.
> application-name#0.0.1 start D:\Applications\New folder\node\chat
> node app.js
init called
info - socket.io started
io
Express server listening on port 3000
GET / 200 312ms - 511
http.js:707
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:707:11)
at ServerResponse.res.setHeader (D:\Applications\New folder\node\chat\node_m
odules\express\node_modules\connect\lib\patch.js:59:22)
at next (D:\Applications\New folder\node\chat\node_modules\express\node_modu
les\connect\lib\proto.js:153:13)
at Function.app.handle (D:\Applications\New folder\node\chat\node_modules\ex
press\node_modules\connect\lib\proto.js:198:3)
at Server.app (D:\Applications\New folder\node\chat\node_modules\express\nod
e_modules\connect\lib\connect.js:66:31)
at Manager.handleRequest (D:\Applications\New folder\node\chat\node_modules\
socket.io\lib\manager.js:564:28)
at Server.<anonymous> (D:\Applications\New folder\node\chat\node_modules\soc
ket.io\lib\manager.js:118:10)
at Server.EventEmitter.emit (events.js:117:20)
at HTTPParser.parser.onIncoming (http.js:2051:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:122:23
)
When i access localhost:3000 in browser all it happens. I am using node version v0.10.10, express 3.4.4.
Got the solution!! It was because of some version mismatch. Here is the dependency that i used
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.4.4",
"socket.io": "0.9",
"jade": "*"
}
}

Resources