0

I'm new to Node.js but willing to give it a serious try. Coming from PHP things seem a bit confusing as there is no index.php, but a start-script needs to be executed to fire up the server npm start.

How is this done in production? Are there pre-run scripts? What if the server closes for some reason, how do I get it back up automatically without having connection problems for the clients? Will it automatically work for the domain, or does it also mean someone alwasy has to go to domain.com:3000?

Ar am I thinking about this the wrong way?

4 Answers 4

5

What you are asking is very broad in term of question . Let me give the idea how it work.

Coming from PHP things seem a bit confusing as there is no index.php, but a start-script needs to be executed to fire up the server npm start.

So in node.js we have a file by which we start our node server and that we decide what we want . mostly people use app.js , server.js , index.js

when you run npm start , that means you would have package.json in the folder that file have written start: node app.js . And when you run npm start , it get fire .

How is this done in production? Are there pre-run scripts?

NODE_ENV=production npm start , you can access this in node code like this process.env.NODE_ENV . in this way you can add dev,qa tag for each environment .

I will recommend you to have a look in

http://pm2.keymetrics.io/

What if the server closes for some reason, how do I get it back up automatically without having connection problems for the clients? 

For that reason you can look at https://nodejs.org/api/cluster.html

You can manage the crash thread and then open another thread as node is single thread. Also you can manage the node.js all type of error By this . This make node.js catch all exception and error https://nodejs.org/api/process.html
process.on

does it also mean someone alwasy has to go to domain.com:3000?

No. You can take any port you want . 80,8080 whatever . I will recommend to use nginx in front of node.js application . But for less compatibility go for simple node application .

for example:-

var http = require('http');
var port = 3000 ; // take any 80, 8080

http.createServer(function (request, response) {
    response.writeHead(200, {
        'Content-Type': 'text/plain',
        'Access-Control-Allow-Origin' : '*'
    });
    response.end('Hello World\n');
}).listen(port);

Hope this help .

Sign up to request clarification or add additional context in comments.

Comments

2

Answering you 1st question there are other options how you can run node application. I suggest that you start using some packages likeNodemon which are actually built for this purpose.

Answering your second question, you can use same for production deployments using some container system if you like. here are some optionsdocker, kubarnetees,and many more.

Your automatic restart thing can be solve either by you container manager or package you have used for deployment.

And for redirecting all request coming on 80 or 443 port that you want to redirect to your application you can try nginx.

Comments

1

There are some modules which you can use to restart the server automatically if it closes for some reason. Some of them are pm2, forever.

Comments

1

Without going into much detail, you have to be VERY clear of the following:

Your node web process will die. Yes, that's right, when there is an uncaught exception it can die. Therefore, you need more than one process for failover, and for that there are many techniques and libraries. Some of them are:

  1. Several node processes load balanced behind a web server (nginx most commonly used)
  2. Managed cluster of node processes (https://github.com/Unitech/pm2)

Or (not that good for production in my opinion) some process monitor which will restart your node web process if it dies: https://github.com/foreverjs/forever

3 Comments

No the point is wrong that node will die . Yes it will die if we not handle uncaught exceptions and they can be fully manage by process in node.js But using process.on is not good practice as we should solve this errors . But if you are good with it you can manage error. If we talk about server , every server will die one day.
Process will also not die , if you have caught-ed all exception . It is the approach we use to make process die . Process can we teamed , the current thread process of node.js .
Thanks for your answer and also the reference to PM2. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.