I used the express generator to create a simple express app, which when started on dev works fine on localhost:3000.
When I push this to elastic beanstalk using the eb command-- git aws.push, however, I get a 502 error on the production server.
Looking into the logs, the error I get is:
2014/04/01 19:29:40 [error] 24204#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.2.178, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "macenvexp-env-hqv9ucmzev.elasticbeanstalk.com"
2014/04/01 19:29:40 [error] 24204#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.2.178, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "macenvexp-env-hqv9ucmzev.elasticbeanstalk.com"
I'm using the default nginx configuration. When I run a node.js sample app without Express, it works fine. Here's the express code in app.js:
var express = require('express');
var http = require('http');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes');
var users = require('./routes/user');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router);
app.get('/', routes.index);
app.get('/users', users.list);
/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
And here's the package.json file:
{
"name": "macEnvExp",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "DEBUG=macEnvExp node bin/www"
},
"dependencies": {
"express": "~3.4.8",
"static-favicon": "~1.0.0",
"morgan": "~1.0.0",
"cookie-parser": "~1.0.1",
"body-parser": "~1.0.0",
"debug": "~0.7.4",
"jade": "~1.3.0"
}
}
And here is bin/www:
#!/usr/bin/env node
var debug = require('debug')('my-application');
var app = require('../app');
app.configure(function(){
app.set('port', process.env.PORT || 3000);
});
console.log(app.get('port'));
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
For clarity, I'll state the answer from the comments.
AWS ELB runs node app.js BEFORE npm start. node app.js doesn't give an error, but it doesn't open any ports.
The solution is to simply rename app.js to anything else except server.js (ie main.js) and reference that in bin/www by pointing to it in the /bin/www file: var app = require('../app'); to var app = require('../main');
Then it should be working correctly!
For clarity, here is what my directory looks like:
The package.json file will get called by ELB when it launches the application server. Here it has the instruction to run the start script node bin/www
This is the bin/www file that gets run. We see the require to ../main and the app.set('port'...)
Then the main.js file that runs the routing and all:
When I created the project, the main.js file was named app.js. The problem this caused was based on the priority ELB start sequences. ELB will launch the application and check first to see if app.js exists -- if it does exist, it runs node app.js, otherwise it will check if package.json exists and try to run npm start.
When the main.js had the name app.js ELB tried to start the whole application by running it. However this file doesn't open any ports.
An alternative to renaming app.js is to create an elastic beanstalk configuration file. Add a .config file into the .ebextensions folder, for example, .ebextensions/34.config. Change the NodeCommand setting in the namespace aws:elasticbeanstalk:container:nodejs to whatever command you want to run to start the server. For example, this is a minimal .config file to run npm start instead of app.js:
option_settings:
- namespace: aws:elasticbeanstalk:container:nodejs
option_name: NodeCommand
value: "npm start"
See http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs_custom_container.html and http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options.html#command-options-nodejs for more information.
Edit:
An even easier way - using the AWS console, Configuration/Software has the "Node command" option - just set that to npm start.
Set running port to 8081
app.set('port', 8081);
Actually, there is another option.
At the Elastic Beanstalk console, inside your app-environment section, there is a Configuration menu item on your left side (right bellow Dashboard menu option). If you click there, you will find many configuration options. Click at Software Configuration and then define which is your node command. There explain the order of commands it tries indeed: "Command to start the Node.js application. If an empty string is specified, app.js is used, then server.js, then "npm start" in that order"
My mistake was at my start command script. It was starting nodemon:
"scripts": {
"start": "NODE_ENV=production && nodemon ./bin/www"
Then I changed to node and it worked:
"scripts": {
"start": "NODE_ENV=production && node ./bin/www"
Hope I helped someone.
If you use port 8081 for running your express app and use sudo for running node server, Your application will be accessed directly from elasticbean url without port numbers, otherwise it will display a 502 Gateway error from nginx.
Nginx proxying 8081 port by default for node app on elastibeanstalk.
Create file: .ebextensions/nodecommand.config and put the option settings below:
option_settings:
aws:elasticbeanstalk:container:nodejs:
NodeCommand: sudo pm2 start server.js (server command with sudo ie. sudo node /bin/www)
You can create another file for container commands: .ebextensions/01_init.config and put the desired commands which will be run before deployment. For example:
container_commands:
01_node_v6_install:
command: sudo curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
02_install_node:
command: sudo yum -y install nodejs
03_npm_install_gulp_webpack:
command: sudo npm install -g gulp webpack pm2
04_npm_install:
command: sudo npm install
05_webpack_run:
command: sudo webpack
In case anyone did the silly thing I did, make sure your bin folder is committed if you are using express. I had mine in my .gitignore file and this is why I was getting a 502 error.
Just remove /bin from .gitignore, commit, and the deploy changes to EB.
new to AWS and been a while since i webdeved, but was stuck tonight on same issue, and thanks to everyone in the thread, i am very happy to say that basic socket.io tutorial works now like a charm, i was just forgetting one line in package.json :
"scripts":
{
"start": "node app.js"
}
oh, and port !
the only thing i kept from elasticbean sample node.js app is this value instead of pure 3000 value :
var port = process.env.PORT || 3000;
Note: I ran into this issue and none of the solutions were working for me.
My solution was to make sure the devDependencies in package.json were actually in dependencies.
For example:
{
"name": "whaler-test",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"create-db": "cd dynamodb && node createDonorsTable.js && cd ..",
"delete-db": "cd dynamodb && node deleteDonorsTable.js && cd ..",
"load-data": "cd dynamodb && node loadDonorsData.js && cd ..",
"read-data": "cd dynamodb && node readDataTest.js && cd .."
},
"dependencies": {
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.16.0",
"http-errors": "~1.6.2",
"jade": "~1.11.0",
"morgan": "~1.9.0",
"nodemon": "1.17.5",
"cors": "2.8.4",
"aws-sdk": "^2.270.1"
}
}
Not:
{
"name": "whaler-test",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"create-db": "cd dynamodb && node createDonorsTable.js && cd ..",
"delete-db": "cd dynamodb && node deleteDonorsTable.js && cd ..",
"load-data": "cd dynamodb && node loadDonorsData.js && cd ..",
"read-data": "cd dynamodb && node readDataTest.js && cd .."
},
"dependencies": {
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.16.0",
"http-errors": "~1.6.2",
"jade": "~1.11.0",
"morgan": "~1.9.0",
"nodemon": "1.17.5"
},
devDependencies {
"cors": "2.8.4",
"aws-sdk": "^2.270.1"
}
}
Related
I am a beginner at node.js and need to host an application that I didn't personally write on an azure server for some testing. The site runs fine locally hosted, as well as hosted using ngrok. Yet, when I host it on azure, I get the following error:
[1] 2020-08-23T00:26:36
Container etuition_0_41152ef3 didn't respond to HTTP pings on port: 8080, failing site start
[2] 2020-08-23T00:26:36
Container etuition_0_41152ef3 for site etuition did not start within expected time limit.
Now I must stress that I am completely unfamiliar with node.js, but to me it seems that the http requests are lining up correctly. Here is the code for my index.js, where I think the problem may lie.
'use strict';
/**
* Load Twilio configuration from .env config file - the following environment
* variables should be set:
* process.env.TWILIO_ACCOUNT_SID
* process.env.TWILIO_API_KEY
* process.env.TWILIO_API_SECRET
*/
require('dotenv').load();
const express = require('express');
const http = require('https');
const path = require('path');
const { jwt: { AccessToken } } = require('twilio');
const VideoGrant = AccessToken.VideoGrant;
// Max. period that a Participant is allowed to be in a Room (currently 14400 seconds or 4 hours)
const MAX_ALLOWED_SESSION_DURATION = 14400;
// Create Express webapp.
const app = express();
// Set up the paths for the examples.
[
'bandwidthconstraints',
'codecpreferences',
'dominantspeaker',
'localvideofilter',
'localvideosnapshot',
'mediadevices',
'networkquality',
'reconnection',
'screenshare',
'localmediacontrols',
'remotereconnection',
'datatracks',
].forEach(example => {
const examplePath = path.join(__dirname, `../examples/${example}/public`);
app.use(`/${example}`, express.static(examplePath));
});
// Set up the path for the quickstart.
const quickstartPath = path.join(__dirname, '../quickstart/public');
app.use('/quickstart', express.static(quickstartPath));
// Set up the path for the examples page.
const examplesPath = path.join(__dirname, '../examples');
app.use('/examples', express.static(examplesPath));
/**
* Default to the Quick Start application.
*/
app.get('/', (request, response) => {
response.redirect('/quickstart');
});
/**
* Generate an Access Token for a chat application user - it generates a random
* username for the client requesting a token, and takes a device ID as a query
* parameter.
*/
app.get('/token', function(request, response) {
const { identity } = request.query;
// Create an access token which we will sign and return to the client,
// containing the grant we just created.
const token = new AccessToken(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_API_KEY,
process.env.TWILIO_API_SECRET,
{ ttl: MAX_ALLOWED_SESSION_DURATION }
);
// Assign the generated identity to the token.
token.identity = identity;
// Grant the access token Twilio Video capabilities.
const grant = new VideoGrant();
token.addGrant(grant);
// Serialize the token to a JWT string.
response.send(token.toJwt());
});
// Create http server and run it.
const server = http.createServer(app);
const port = process.env.PORT || 8080;
server.listen(port, function() {
console.log('Express server running on *:' + port);
});
Here is my package.json
{
"name": "video-quickstart-js",
"version": "1.0.0-dev",
"description": "Twilio Video SDK Quick Start for JavaScript",
"main": "index.js",
"scripts": {
"build": "npm-run-all build:* ",
"build:examples": "npm-run-all build:examples:*",
"build:examples:bandwidthconstraints": "copyfiles -f examples/bandwidthconstraints/src/helpers.js examples/bandwidthconstraints/public && browserify examples/bandwidthconstraints/src/index.js > examples/bandwidthconstraints/public/index.js",
"build:examples:codecpreferences": "copyfiles -f examples/codecpreferences/src/helpers.js examples/codecpreferences/public && browserify examples/codecpreferences/src/index.js > examples/codecpreferences/public/index.js",
"build:examples:dominantspeaker": "copyfiles -f examples/dominantspeaker/src/helpers.js examples/dominantspeaker/public && browserify examples/dominantspeaker/src/index.js > examples/dominantspeaker/public/index.js",
"build:examples:localvideofilter": "copyfiles -f examples/localvideofilter/src/helpers.js examples/localvideofilter/public && browserify examples/localvideofilter/src/index.js > examples/localvideofilter/public/index.js",
"build:examples:localvideosnapshot": "copyfiles -f examples/localvideosnapshot/src/helpers.js examples/localvideosnapshot/public && browserify examples/localvideosnapshot/src/index.js > examples/localvideosnapshot/public/index.js",
"build:examples:mediadevices": "copyfiles -f examples/mediadevices/src/helpers.js examples/mediadevices/public && browserify examples/mediadevices/src/index.js > examples/mediadevices/public/index.js",
"build:examples:networkquality": "copyfiles -f examples/networkquality/src/helpers.js examples/networkquality/public && browserify examples/networkquality/src/index.js > examples/networkquality/public/index.js",
"build:examples:reconnection": "copyfiles -f examples/reconnection/src/helpers.js examples/reconnection/public && browserify examples/reconnection/src/index.js > examples/reconnection/public/index.js",
"build:examples:screenshare": "copyfiles -f examples/screenshare/src/helpers.js examples/screenshare/public && browserify examples/screenshare/src/index.js > examples/screenshare/public/index.js",
"build:examples:localmediacontrols": "copyfiles -f examples/localmediacontrols/src/helpers.js examples/localmediacontrols/public && browserify examples/localmediacontrols/src/index.js > examples/localmediacontrols/public/index.js",
"build:examples:remotereconnection": "copyfiles -f examples/remotereconnection/src/helpers.js examples/remotereconnection/public && browserify examples/remotereconnection/src/index.js > examples/remotereconnection/public/index.js",
"build:examples:datatracks": "copyfiles -f examples/datatracks/src/helpers.js examples/datatracks/public && browserify examples/datatracks/src/index.js > examples/datatracks/public/index.js",
"build:quickstart": "browserify quickstart/src/index.js > quickstart/public/index.js",
"clean": "npm-run-all clean:*",
"clean:examples": "npm-run-all clean:examples:*",
"clean:examples:bandwidthconstraints": "rimraf examples/bandwidthconstraints/public/index.js examples/bandwidthconstraints/public/helpers.js",
"clean:examples:codecpreferences": "rimraf examples/codecpreferences/public/index.js examples/codecpreferences/public/helpers.js",
"clean:examples:dominantspeaker": "rimraf examples/dominantspeaker/public/index.js examples/dominantspeaker/public/helpers.js",
"clean:examples:localvideofilter": "rimraf examples/localvideofilter/public/index.js examples/localvideofilter/public/helpers.js",
"clean:examples:localvideosnapshot": "rimraf examples/localvideosnapshot/public/index.js examples/localvideosnapshot/public/helpers.js",
"clean:examples:mediadevices": "rimraf examples/mediadevices/public/index.js examples/mediadevices/public/helpers.js",
"clean:examples:networkquality": "rimraf examples/networkquality/public/index.js examples/networkquality/public/helpers.js",
"clean:examples:reconnection": "rimraf examples/reconnection/public/index.js examples/reconnection/public/helpers.js",
"clean:examples:screenshare": "rimraf examples/screenshare/public/index.js examples/screenshare/public/helpers.js",
"clean:examples:localmediacontrols": "rimraf examples/localmediacontrols/public/index.js examples/localmediacontrols/public/helpers.js",
"clean:examples:remotereconnection": "rimraf examples/remotereconnection/public/index.js examples/remotereconnection/public/helpers.js",
"clean:examples:datatracks": "rimraf examples/datatracks/public/index.js examples/datatracks/public/helpers.js",
"clean:quickstart": "rimraf quickstart/public/index.js",
"start": "npm run clean && npm run build && node server"
},
"repository": {
"type": "git",
"url": "git+https://github.com/twilio/video-quickstart-js.git"
},
"keywords": [
"twilio",
"video",
"chat",
"ip",
"real",
"time",
"diggity"
],
"author": "Twilio",
"license": "MIT",
"bugs": {
"url": "https://github.com/twilio/video-quickstart-js/issues"
},
"homepage": "https://github.com/twilio/video-quickstart-js#readme",
"dependencies": {
"dotenv": "^4.0.0",
"express": "^4.15.2",
"prismjs": "^1.6.0",
"stackblur-canvas": "^1.4.0",
"twilio": "^3.19.1",
"twilio-video": "^2.7.0"
},
"devDependencies": {
"browserify": "^14.3.0",
"copyfiles": "^1.2.0",
"npm-run-all": "^4.0.2",
"rimraf": "^2.6.1"
}
}
Thank you for reading!
First, make sure what services you use, AWS or Azure Web App Services ?
Whatever services you use, I recommand you use git to deploy your web app.
Use git to deploy in azure web app services.
Use git to deploy in aws.
You just make sure your web app can run successfully in local. And the port you use like app.set('port', process.env.PORT || 3000); or const port = process.env.PORT || 3000. Which means you can success run in local with 3000 port.
For more details, you can see my answer in another post.
Azure - Unhandled Exception: System.IO.FileNotFoundException
Concurrently JS application pipeline install and build hangs (Express js for server, Create-React-App for Client)
You can refer to the way of troubleshooting when deploy web app by Action. Hope my answer can help you.
I am using the example setup from the https://www.npmjs.com/package/express-subdomain package. However, when I try api.localhost:3000 in a browser it throws an error: "Cannot GET /"
How can I get api.localhost:3000 running? I would consider other solutions, not just ones using express-subdomain as long as you don't have to type in the fqdn in the setup.
app.js:
var subdomain = require('express-subdomain');
var express = require('express');
var app = express();
// *** Code examples below go here! ***
var router = express.Router();
//api specific routes
router.get('/', function(req, res) {
res.send('Welcome to our API!');
});
router.get('/users', function(req, res) {
res.json([
{ name: "Brian" }
]);
});
app.use(subdomain('api', router));
app.listen(3000);
package.json:
{
"name": "auth_manager",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.3",
"express-subdomain": "^1.0.5"
}
}
I think that the API is alive at http://api.example.com/ and not in the api.localhost:3000. However if you want to run them locally I suggest that you follow https://www.npmjs.com/package/express-subdomain#developing-locally
If you plan to use this middleware while developing locally, you have to ensure that your subdomain is listed in your hosts file. Refer here.
On MacOS:
Adjust host file to forward requests from url:
sudo nano /etc/hosts
127.0.0.1 myapp.dev
127.0.0.1 api.myapp.dev
Clear cache:
sudo killall -HUP mDNSResponder
In express:
app.use(subdomain('api', router));
Any authentication middleware needs to be adjusted to authenticate api.myapp.dev not localhost
In Chrome you can use the api.myapp.dev url
If you are using SSL (https) and have a self signed cert: Chrome usually blocks the hostfile URL if it is not localhost. Then, you need to click anywhere in the Chrome window and literally type in thisisunsafe and it will let you through. Refer to this ticket.
I have a node/socket.io chat app hosted on openshift, and while it starts correctly if i ssh into the server and do "node main.js" (where main.js is the server script that starts the chat), I can't start the app on the server by web interface, where it would go on automatically; If i just start the app by ssh, it would stop working as soon as i exit the terminal.
I get this error when starting the app by the web interface:
Starting Node.js application...
Application is already stopped.
Warning! Could not start Node.js application!
Failed to execute: 'control restart' for /var/lib/openshift/57003fbe7628e1491d00011e/nodejs
In case it's relevant, my package.json file is
{
"name": "rainychat",
"version": "1.0.0",
"description": "rainychat, my chat app",
"main": "main.js",
"dependencies": {
"express": "^4.13.4",
"socket.io": "^1.4.5",
"validator": "^5.1.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "JG",
"license": "ISC"
}
And here you can see the files of the app by ftp:
I can't decode what that error means...
My main.js code
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/chat.html'); // /home/redadmin/public_html/rainychat.com
console.log('enviado');
});
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.set('ip', process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1');
http.listen(app.get('port'), app.get('ip'), function () {
console.log('Listening on port ' + app.get('port'));
});
//... More code
If you're creating a new Node project, start with npm init to create the package.json file. You can add the --auto option to give it safe defaults.
Remember, the JSON file must be valid JSON, so test it with jsonlint or a tool like an online validator.
Any dependencies your project has should be spelled out in the package file. This is done automatically with things like npm install express --save.
Before I start explaining my error, let me say I'm a Windows user and don't have a lot of experience using Unix commands. So each of these steps are done using the Docker Quickstart Terminal (MINGW64).
It was a few weeks ago I first heard about docker and thought using it for a node/express website. So I installed the Docker package on my Synology server.
After finishing up the website I've done the following:
I followed the instructions on this website:
https://docs.docker.com/windows/step_one/ Up until the last step,
everything worked (including docker run hello-world)
Then it was onto "Dockerizing a Node.js web app":
https://docs.docker.com/engine/examples/nodejs_web_app/
File hierarchy:
src (C:.....\projectname)
--assets (folder)
--controllers (folder)
--public (folder)
--src (folder)
--util (folder)
--views (folder)
--node_modules (folder)
--bin (folder)
----www (file, no extention)
--app.js (file)
--Dockerfile (file, no extention)
--package.json (file)
As for content:
www:
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('projectname:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
app.js:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(require('./controllers'));
};
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
package.json:
{
"name": "projectname",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"bcrypt-nodejs": "0.0.3",
"body-parser": "~1.13.2",
"bookshelf": "^0.9.2",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"express": "~4.13.1",
"express-session": "^1.13.0",
"i18n": "^0.8.0",
"jade": "~1.11.0",
"knex": "^0.10.0",
"morgan": "~1.6.1",
"mysql": "^2.10.2",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"serve-favicon": "~2.3.0"
},
"devDependencies": {
"autoprefixer": "^6.3.3",
"browserify": "^13.0.0",
"connect-livereload": "^0.5.4",
"grunt": "^0.4.5",
"grunt-browserify": "^4.0.1",
"grunt-contrib-cssmin": "^0.14.0",
"grunt-contrib-sass": "^0.9.2",
"grunt-contrib-uglify": "^0.11.1",
"grunt-contrib-watch": "^0.6.1",
"grunt-postcss": "^0.7.2"
}
}
So as you can see in the package.json and www (which was generated by the express generator command), I have to write npm start to run the node/express server.
Dockerfile:
FROM centos:centos6
RUN yum install -y epel-release
RUN yum install -y nodejs npm
COPY package.json /projectname/package.json
RUN cd /projectname; npm install --production
COPY . /projectname
EXPOSE 8080
CMD ["npm", "start"]
After a successful build docker build -t username/projectname . I do get a SECURITY WARNING:
Successfully built 5ed562273b56
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
Apart from that no errors are thrown, so I ran the image: docker run -p 49160:8080 -d username/projectname. After which I get a long hash string
de297db51ab6fb3f842abb58267c1e189d2b9de51715a619a2f5431e868dc54f
Still following the principles on https://docs.docker.com/engine/examples/nodejs_web_app/. To test the image I listed the container id using docker ps which got me this:
CONTAINER ID | IMAGE | COMMAND | CREATED | STATUS | PORTS | NAMES
So completely empty! Nothing, except for the headers of the table... But when I use the code provided in the link and build their image it does give me a result (as stated in the article):
CONTAINER ID | IMAGE | COMMAND | CREATED | STATUS | PORTS | NAMES
26d3ac309d81 | username/centos-node-testing | "node /src/index.js" | 35 minutes ago | Up 35 minutes | 0.0.0.0:49160->8080/tcp | gigantic_ritchie
Just to be sure, I tried calling the app using the curl-command: curl -i 192.168.99.100:49160. Unfortunately that gave me an error:
curl: (7) Failed to connect to 192.168.99.100 port 49160: Connection refused
The ip address is retrieved using the docker-machine ip command.
As a last resort, someone suggested to simply run the app using following command docker run username/projectname. That however gave me an error:
npm ERR! Error: ENOENT, open '/package.json'
npm ERR! If you need help, you may report this log at:
npm ERR! http://github.com/isaacs/npm/issues
npm ERR! or email it to:
npm ERR!
npm ERR! System Linux 4.1.19-boot2docker
npm ERR! command "node" "/usr/bin/npm" "start"
npm ERR! cwd /
npm ERR! node -v v0.10.42
npm ERR! npm -v 1.3.6
npm ERR! path /package.json
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /npm-debug.log
npm ERR! not ok code 0
Any ideas what might cause this?
Your container is non-existent because the command you've provided (CMD, above) is returning a non-zero exit status, and the container is destroyed due to failure. In your Dockerfile, let's please try something like the following, which should ensure that npm start is run from within your project root:
FROM centos:centos6
RUN yum install -y epel-release
RUN yum install -y nodejs npm
COPY package.json /projectname/package.json
# Set the working directory
WORKDIR /projectname
RUN npm install --production
COPY . /projectname
EXPOSE 8080
CMD ["npm", "start"]
Also, for future, you might have luck troubleshooting a container if you use docker run -it username/projectname /bin/bash.
I created a node app in openshift, I connected through SSH, and I was able to push my code and I could change the server.js code for a simple hello world.
Server.js
#!/bin/env node
var http = require('http');
var serverIp = process.env.OPENSHIFT_NODEJS_IP;
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
//creating server
var server = http.createServer(function(req, res) {
res.writeHead('Content-Type', 'text/plain');
res.end('Simple example!!');
});
//listening
server.listen(port, serverIp, function() {
console.log('Server started on port ' + port + ' IP: ' + serverIp);
});
When push this new code, I'm not getting any errors.
And this is the package.json file
{ "name": "Hello_world",
"version": "1.0.0",
"description": "Hello world",
"engines": {
"node": ">= 0.6.0",
"npm": ">= 1.0.0"
},
"devDependencies": {},
"bundleDependencies": [],
"private": true,
"scripts": {
"start" : "node server.js"
},
"main": "server.js"
}
When I do this, the application gets very very slow (like 2/3 minutes of waiting), here's the link.
[http://avalecia-minisis.rhcloud.com/]enter code here1
But when I change the code for the original, everything's fine... :/
I don't see where the issue could be.
When deploying to OpenShift, your application's build process can be optimized (or tuned) in a variety of ways.
If you'd like to minimize downtime between deploys, you can try enabling the hot_deploy feature:
mkdir .openshift ; mkdir .openshift/markers ; touch .openshift/markers/hot_deploy
git add .openshift/markers/hot_deploy
git commit -m "enabling the hot_deploy marker to minimize downtime on OpenShift"
git push
Checking in your node_modules folder can also have a major impact on build time.
Turning on NPM_CONFIG_PRODUCTION is another approach that may help (see : Run npm install --production on OpenShift)