Heroku Foreman Exits on Express.js bodyParser() call - javascript

When running foreman start, I see the following message
> foreman start
20:38:55 web.1 | started with pid 3896
20:38:55 web.1 | Development
20:38:56 web.1 | connect.multipart() will be removed in connect 3.0
20:38:56 web.1 | exited with code 1
20:38:56 system | sending SIGKILL to all processes
I was wondering why this was the case, since running node server.js does not appear to terminate the server.
Here is the segment of code that appears to cause the app to terminate with exit code 1:
var app = express();
app.configure(function()
{
// More config things above
app.use(express.bodyParser()); // This line is causing the issue
// More config things below
}
The above is code using the Express.js framework. Removing the above call to express.bodyParser() allows the server to run (via foreman). The problem is, I'll need the body parser module in order to parse my incoming get/posts requests.
Any help on this issue would be much appreciated.

I have no idea why foreman exits when deprecation warning is reported in express, but you can eliminate this behavior replacing app.use(express.bodyParser()); with
app.use(express.json());
app.use(express.urlencoded());
connect.multipart() will be removed from bodyParserin next version of Connect and this is probably the issue. You can find more info in Connect documenattion and/or in this StackOverflow Q&A.

Related

How to execute a Node.js script on the server?

I'm beginning to learn web technologies and programming and I'm setting up my own local webserver. I have HTTPD, PHP, Python, MySQL all up and running on Windows. Now I want to add Node.js to the mix. I installed the Windows 64 bit installer. Now how do I begin? I have a basic Hello World script in a test.js file. But when I access this file in the browser it only displays as plain text. It's not executed. How to execute a Node.js script on the server?
Starting a node script is pretty simple. Just use your command line or terminal and execute the following command.
node /path/to/your/file.js
Doing so you'll start your node script. If you're going to start a server it's pretty much the same. Just keep in mind to define a server in your node file and start it. A simple server using express could look like this (You can also use a fully node way, this is just a simple example using express. You may check google for how to set up a simple node http server).
var express = require('express');
var app = express();
var port = 4000;
app.listen(process.env.port || port);
As you can see the specified port is set to 4000. You can simply adjust this by changing the value itself or passing in a node environment variable. To pass in an environment variable just start your server like this.
node port=3000 /path/to/your/file.js
This will finally pass the value of port to process.env.port which obviously will start your server on the port 3000.
you can use these packages to keep the file running so you won't have to login to server every time :-
forever
and you can just write :-
forever start app.js
nodemon
nodemon app.js
pm2
which is very useful , as it will auto restart your app when it crash or any error happens
pm2 start app.js
Your run your file: node server.js
Then it starts.
There should be specified in code on which port you run your server. Then it is accessible for example at http://localhost:3000/
As Quentin noted, I was thinking about "creating web server". Of course, you can run javascript code with Node.js without server. Then skip the part abot localhost, just use node test.js in console.

Node + MongoDB issue EADDRINUSE

Trying to build an experimental CRUD app, however there is an issue connecting to db.
Using mLab free db.
No matter which port is specified the error is EADDRINUSE :::
How do I close all Node instances that might be using these ports?
Solution is already given here.
How to fix Error: listen EADDRINUSE while using nodejs?
To check which ports are in use , try this command
netstat -ntlp
to check node JS processes (instance of your app or other using the same port yu are requesting ) running
ps aux | grep -i node
and kill relevant processes by
kill *process-id* | killall node

MONGO_URL for running multiple Meteor apps on one server

I have one Meteor application running on my Ubuntu server (Digital Ocean). I use Meteor Up (MUP) to deploy and keep the app running. Everything works fine.
However, when I try to deploy a second app on the same server, something goes wrong in connecting to the MongoDB. I get a long and unreadable error message that starts "Invoking deployment process: FAILED" and then ends with
Waiting for MongoDB to initialize. (5 minutes)
connected
myapp start/running, process 25053
Waiting for 15 seconds while app is booting up
Checking is app booted or not?
myapp stop/waiting
myapp start/running, process 25114
And the app refuses to run. I have tried a number of things to fix this and will edit this post if more info is requested, but I'm not sure what's relevant. Essentially I don't understand the Error message, so I need to know what the heck is going on?
EDIT:
I want to add that my app runs fine if I go into the project folder and use the "meteor" command. Everything runs as expected. It is only when I try to deploy it for long-term production mode with MUP that I get this error.
EDIT:
I moved on to trying mupx instead of mup. This time I can't even get past the installation process, I get the following error message:
[Neal] x Installing MongoDB: FAILED
-----------------------------------STDERR-----------------------------------
Error response from daemon: no such id: mongodb
Error: failed to remove containers: [mongodb]
Error response from daemon: Cannot start container c2c538d34c15103d1d07bcc60b56a54bd3d23e50ae7a8e4f9f7831df0d77dc56: failed to create endpoint mongodb on network bridge: Error starting userland proxy: listen tcp 127.0.0.1:27017: bind: address already in use
But I don't understand why! Mongod is clearly already running on port 27017 and a second application should just add a new database to that instance, correct? I don't know what I'm missing here, why MUP can't access MongoDB.
It's tricky without your mup.json to see what's going on here. Given what you said, it looks like your 2nd app deployment tries to override/boot mongodb over the 1st one which is locked, the mongodb environment fails to boot, causing then the fail. You should tackle this different ways:
If your objective is to share your mongoDB, point the MONGO_URL from your 2nd mup.jon on your first mongodb instance. It's generally something along the 2701X ports. As it's a shared DB, changes in one database could affect the other.
meteor-up oversees the deployment of your app from a meteor-nice-to-test thing to a node+mongodb environment. You can spawn another mongod instance with :
mongod --port 2701X --dbpath /your/dbpath --fork --logpath /log/path on your DO server and then point MONGO_URL there.
Last but not least, mupx having docker under the hood. Using mupx for your deployments should isolate both apps from each other.

sails debug command not working in Sails.js

I am creating my first sails.js app. When I tried
sails debug
I'm getting the following error on my command prompt
Debugger listening on port 5858
info: Starting app...
error: Grunt :: Error: listen EADDRINUSE
at exports._errnoException (util.js:746:11)
at Agent.Server._listen2 (net.js:1129:14)
at listen (net.js:1155:10)
at Agent.Server.listen (net.js:1240:5)
at Object.start (_debugger_agent.js:20:9)
at startup (node.js:86:9)
at node.js:814:3
To get the PID of the process using port:5858, I tried running
C:\Windows\system32>netstat -a -n -o
but unfortunately there is no process bound to port 5858. Am I missing something here?
I'm using Windows 8.1 with node.js v0.12.0 and sails.js 0.11.0
My server uses node 0.10.38 with sails because of some weird unfixed grunt thing with 11+. Haven't pulled up this issue in a while, but it looks like there's new activity... check out this comment in particular, which explains the issue and a possible fix (direct quote):
Possible Solution:
Looking at the options for child_process.fork, the --debug flag is being passed down to the child upon exiting the womb i.e. running
sails debug :
// ./node_modules/sails/bin/sails-debug.js
// Spin up child process for Sails
Womb.spawn('node', ['--debug', pathToSails, 'lift'], {
stdio: 'inherit'
});
setting options.execArgv to an empty array removes the flag and allows the process to continue:
// ./node_modules/sails/lib/hooks/grunt/index.js
var child = ChildProcess.fork(
path.join(__dirname, 'grunt-wrapper.js'),
[
taskName,
'--pathToSails='+pathToSails,
'--gdsrc='+ pathToSails + '/node_modules'
],
{
silent: true,
stdio: 'pipe',
execArgv: []
}
);
It seems like a bug in Sails. You can apply the fix your self by replacing your Sails' file:
./node_modules/sails/lib/hooks/grunt/index.js
with the contents of the following:
https://raw.githubusercontent.com/balderdashy/sails/88ffc0ed9949f8c74ea390efb5610b0e378fa02c/lib/hooks/grunt/index.js
This is the file that will be in the Sails' release v12.
Did you try to run in debug like simple node.js?
node --debug app.js

Why can't I run node.js & socket.io in a screen or with Forever?

I've got some annoying problem and that is I have trouble in running nodejs in a screen. Because as fast as I leave the screen and no sockets are connected the next whom is to connect will be faced by an error message until the screen is opened again. As soon as the screen is open with screen -R node (for example) it will start accepting connections again.
However notice that if the screen is closed (running in the background) and someone already has a socket open, it will continue to answer new connections.
When I try to start the application again, with the command node app I get the following message:
module.js:340
throw err;
^
Error: Cannot find module '(unreachable)/square_defense/app'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Server side code:
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(3000);
app.get('/', function (req, res) {
console.log(__dirname);
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.on('user-message', function (data) {
console.log(data);
sendMessage.call(socket, data.message);
});
});
var sendMessage = function(message) {
this.emit('server-message', {message: message});
this.broadcast.emit('server-message', {message: message});
}
I have tried a lot of things like npm install app in directory .. from the app.js. Can't understand why it dont want to work. I really just want a server which I can use for testing purposes that do not crash.
EDIT: I think this has something to do with an encrypted home folder?
I think the unreachable folder is where the problem resides. Error: Cannot find module '(unreachable)/square_defense/app'. But I don't know why this is happening. Right now I am running the application with Forever and as soon as I have an ssh-session on it will work. But as soon as I exit the connection and reload the page. It will generate an error.
As you said, your home directory is encrypted, and according to the document from Ubuntu:
This directory is automatically mounted on login, and unmounted on logout.
So your guess is correct, this is exact what's causing the error. Running a detached screen session won't be considered by the OS as the user are still logged in*. The time all shell sessions of the user ends, the encrypted home directory got unmounted.
Solution: You can move your app to a directory other than $HOME, then I think either GNU Screen or forever will do the job without errors.†
* You can test this by your self: login, start a screen session, run something like vim, detach; use command w to see who is logged in and what they are doing
† I think if you write your own upstart scripts for this, it is still crucial that the app files are not placed in auto-encrypted home directory.
Since you already are on an Ubuntu box, i recommend using Upstart and monit as described in this post: http://howtonode.org/deploying-node-upstart-monit
The only thing i added into the mix was a frontend proxy-server (HAProxy in my case, but nginx works as well) in order to serve my application on port 80.
I deployed a socket.io/express app using this technology stack on Amazon EC2 myself about a month ago, with amazing results!
EDIT:
Here are some resources i collected over time concerning a proper production setup using node.js/nginx/monit/git:
http://cuppster.com/2011/05/12/diy-node-js-server-on-amazon-ec2/ (an awesome guide, helped me a lot!)
http://howtonode.org/deploying-node-upstart-monit
I really recommend the first one, you might have to skip some parts (like npm module installation or system setup), but the parts about nginx/git and supervisor are worth a read.

Categories