I have a valid node.js app that I want to deploy to AWS EB. It is valid because it is working on my localhost by:
npm start
The zip that I upload looks like this:
My app.js looks like:
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 mysql = require('mysql');
var sequelize = require('sequelize');
var routes = require('./routes/index');
var users = require('./routes/users');
var responseDTO = require('./dto/httpResponseDTO.js');
var app = express();
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// 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
app.use(function(err, req, res, next) {
res.status(err.status || 500);
msg = responseDTO.toReseponseDTO(false, err.name, err.errors);
res.json(msg);
});
module.exports = app;
EDIT
The package.json looks like:
{
"name" : #projectname#,
"version" : "0.1.1",
"private" : true,
"scripts" : {
"start" : "node ./bin/www"
},
"dependencies" : {
...
}
}
And the file that the script tries to run (./bin/www) looks like this:
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('mee:server');
var http = require('http');
var models = require('../models');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '8081');
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);
}
It looks like this is causing problem to EB, which says:
Impaired services on all instances.
The error log says:
Server running at http://127.0.0.1:8081/
Server running at http://127.0.0.1:8081/
npm ERR! Linux 3.14.48-33.39.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v0.12.6-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v0.12.6-linux-x64/bin/npm" "start"
npm ERR! node v0.12.6
npm ERR! npm v2.11.2
npm ERR! path /var/app/current/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! enoent ENOENT, open '/var/app/current/package.json'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! Please include the following file with any support request:
npm ERR! /var/app/current/npm-debug.log
npm ERR! Linux 3.14.48-33.39.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v0.12.6-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v0.12.6-linux-x64/bin/npm" "start"
npm ERR! node v0.12.6
npm ERR! npm v2.11.2
npm ERR! path /var/app/current/package.json
npm ERR! code ENOENT
npm ERR! errno -2
It looks like the package.json isn't found? but from the folder structure it is there...
Please let me know what went wrong, it has been bothering me for a long time!
From the code you posted I cannot see the part when you actually start the server but from the logs I see that it starts in port 8081 which it is actually strange to me, I guess in EBS it must have a dynamic PORT
When you select the port you should use process.env.PORT || 8081 this will get the port set by EBS or use 8081 (in local for example).
Amazon has a good guide which should explain you how to deploy there a node app: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs_express.html
Turned out for some reason, when I remote into the linux ec2 instance used by the EB, and try node --version or sudo node --version, it says command not found. I have to manually install node and npm and manually start the server. It's weird that I have to do this though..
Related
I am running into the error
An error occurred in the application and your page could not be served.
when running 'heroku open' command. On heroku dashboard it says deployed successfully but then it will not run 'it is working' from the app.get line of code.
server.js
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex');
const register = require('./controllers/register');
const signin = require('./controllers/signin');
const profile = require('./controllers/profile');
const image = require('./controllers/image');
const db = knex({
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'benjohnson',
password : '',
database : 'smart-brain'
}
});
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.get('/', (req, res)=> { res.send('its working!') })
app.post('/signin', signin.handleSignin(db, bcrypt))
app.post('/register', (req, res) => { register.handleRegister(req, res, db, bcrypt) })
app.get('/profile/:id', (req, res) => { profile.handleProfileGet(req, res, db)})
app.put('/image', (req, res) => { image.handleImage(req, res, db)})
app.post('/imageurl', (req, res) => { image.handleApiCall(req, res)})
app.listen(process.env.PORT || 3000, ()=> {
console.log(`app is running on port ${process.env.PORT}`);
})
When running the server.js file within the heroku cli i receive the error, when running 'heroku logs --tail' i receive this error?
> node#1.0.0 start /Users/benjohnson/.Trash
> nodemon server.js
sh: nodemon: command not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! node#1.0.0 start: `nodemon server.js`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the node#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likel
y additional logging output above.
npm WARN Local package.json exists, but node_modules missing, di
d you mean to install?
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/benjohnson/.npm/_logs/2019-09-25T14_11_11_91
0Z-debug.log
The error is about nodemon.
Open the package.json, is at the same folder with server.js
Find:
"scripts": {
"start": " nodemon server.js",
},
And replace it with:
"scripts": {
"start": "node server.js",
"start:dev": "nodemon server.js"
},
Upload again in Heroku.
When you want to run the project locally, just run in your terminal npm start:dev and it will load server.js with nodemon.
i testing traffic on my node app in localhost using http.get(). i generate 10000 request on every 5 seconds.
the problem is when i run command npm start and after 20 to 30 seconds server crashed and with following error
events.js:136
throw er; // Unhandled 'error' event
^
Error: connect EADDRNOTAVAIL localhost:8000 - Local (localhost:0)
at Object._errnoException (util.js:999:13)
at _exceptionWithHostPort (util.js:1020:20)
at internalConnect (net.js:987:16)
at net.js:1087:9
at process._tickCallback (internal/process/next_tick.js:150:11)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! app#1.0.0 start: `node ./bin/run`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the app#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/kd/.npm/_logs/2018-03-28T08_32_26_105Z-debug.log
i using 16.04.1-Ubuntu
here is my app.js
var rob = require('./test/rob.js');
for (var i = 0; i < 10000; i++) {
rob.init();
}
here is rob.js
var http = require('http');
module.exports = {
init : function(){
var pages = [
'dashboard',
'home',
'login',
'contact',
'support',
'about'
];
setInterval(function(){
try{
var pg = pages[Math.round(Math.random() * 5)];
http.get('http://localhost:8000/' + pg,
res => {
console.log('RES =>','SUCCSESS');
},
err => {
console.log('ERROR','ERROR');
});
}catch(err){
console.log('ER','ERR');
}
},5000);
}
}
here is run.js
var http = require('http');
var app = require('../app');
/* Create Server */
var port = 8000;
var host = '0.0.0.0';
var server = http.createServer(app);
io = module.exports = require('socket.io').listen(server, {
pingTimeout: 7000,
pingInterval: 10000
});
io.set("transports", ["xhr-polling","websocket","polling"]);
server.listen(port,host,function(){
log('server is running on ' + host +':'+port);
});
how to fix this problem ?
there are any other way to check traffic ?
I have an express nodejs server with express-generator. After calling "npm start" server starts properly but I'm not able to close the connection to localhost. Typical exiting the process with "ctrl+c" doesn't work as expected. After "ctrl+c" I'm not able to start the server on the same localhost port again. I'm still using Win7, my node is up to date and other globally installed packages. My console prints out following:
$ npm start
> myapp#0.0.0 start C:\Users\Konrad\dev\myapp
> node ./bin/www
Port 3000 is already in use
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! myapp#0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the myapp#0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Konrad\AppData\Roaming\npm-cache\_logs\2018-01-19T20_02_46_983Z-debug.log
Thats the code in bin/www:
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('myapp: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);
}
So, to avoid this issue I started using the package called "nodemon". It refreshes the code of my application without the need to restart the server.
The issue remains unsolved but I hope nobody else encounters this problem. It was probably my mistake combined with some windows7 incompatibility.
Im doing migration to parse server. And until now everything worked well. But now I want to add google cloud storage file adapter and if I add this line to my code it stops working. Can you somebody tell me why.
var GCSAdapter = require('parse-server-gcs-adapter');
This is the log:
npm ERR! Linux 3.13.0-105-generic
npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
npm ERR! node v6.9.1
npm ERR! npm v3.10.8
npm ERR! code ELIFECYCLE
npm ERR! parse-server-example#1.4.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the parse-server-example#1.4.0 start script 'node index.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the parse-server-example package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node index.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs parse-server-example
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls parse-server-example
npm ERR! There is likely additional logging output above.
2017-01-29T16:56:58.161879+00:00 app[web.1]:
npm ERR! Please include the following file with any support request:
npm ERR! /app/npm-debug.log
My index.js file:
// Example express application adding the parse-server module to expose Parse
// compatible API routes.
var express = require('express');
var ParseDashboard = require('parse-dashboard');
var ParseServer = require('parse-server').ParseServer;
var GCSAdapter = require('parse-server-gcs-adapter');
var path = require('path');
var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;
if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'myAppId',
masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it secret!
serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse', // Don't forget to change to https if needed
// liveQuery: {
// classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
// }
// filesAdapter: new GCSAdapter(
// "oval-proxy-117311",
// "./SymboloKey",
// "Symbolo",
// {directAccess: true}
// )
});
// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey
const dashboard = new ParseDashboard( {
'allowInsecureHTTP': true,
'apps': [
{
'serverURL': process.env.SERVER_URL,
'appName': 'Symbolo',
'appId': process.env.APP_ID,
'masterKey': process.env.MASTER_KEY
}
],
'users': [
{
'user': 'public',
'pass': 'qwerty'
}
]
}, true )
var app = express();
// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));
// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);
// serve the Parse Dashboard
app.use('/dashboard', dashboard)
// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res) {
res.status(200).send('I dream of being a website. Please star the parse-server repo on GitHub!');
});
// There will be a test page available on the /test path of your server url
// Remove this before launching your app
app.get('/test', function(req, res) {
res.sendFile(path.join(__dirname, '/public/test.html'));
});
var port = process.env.PORT || 1337;
var httpServer = require('http').createServer(app);
httpServer.listen(port, function() {
console.log('parse-server-example running on port ' + port + '.');
});
// This will enable the Live Query real-time server
// ParseServer.createLiveQueryServer(httpServer);
If I run npm install:
Klemens-MBP-2:Cloud klemen$ npm install
npm WARN deprecated mongodb#2.2.10: Please upgrade to 2.2.19 or higher
> bcrypt#1.0.2 install /Users/klemen/Dev/Newcomm/Cloud/node_modules/bcrypt
> node-pre-gyp install --fallback-to-build
node-pre-gyp ERR! Tried to download(404): https://github.com/kelektiv/node.bcrypt.js/releases/download/v1.0.2/bcrypt_lib-v1.0.2-node-v51-darwin-x64.tar.gz
node-pre-gyp ERR! Pre-built binaries not found for bcrypt#1.0.2 and node#7.4.0 (node-v51 ABI) (falling back to source compile with node-gyp)
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.