How can I deploy a Node.js application? - node.js

Node.js is "JavaScript on the server". OK. But, how can I "deploy" a Node.js application? Which kind of web server should I use? How can I create "Controllers"? And how can I persist data in a DB? Thanks in advance.

This is one of the best places to get familiar with what's currently available: https://github.com/joyent/node/wiki/modules.
Regarding "which kind of web server" you should use, it depends on what you're trying to build. I currently use express, which I've been very happy with.
For database connectivity, that depends on what type of database you're connecting to. For MongoDB, I use Mongoose, and for CouchDB I just use a raw HTTP client. If you need MySQL, the most popular one right now seems to be node-mysql. There are lots of other database drivers here.
Given the high-level nature of your question, it sounds like you might be better profited by some "getting started" guides, to really get familiar with what node.js is. There are several good articles here, for example. From there you can move into web servers and database drivers more comfortably.

There are many deployment solutions available, CloudFoundry being one of them. I think you need a great understanding of how Node works first though. Basically, to "deploy" an application, you would typically send the files over to the server and run it from the commandline:
node server.js
There is no web server involved like Apache or nginx. If your application needs a web server, there are some solutions in Node like Express.
Databases work as usual. You install one over to your server, use one of the many Node modules to connect to it and write data. It's separate from your Node server.
Check out this excellent list of Node modules from the GitHub wiki.

You should start by looking at http://nodejs.org. Great place to find information when writing code. There are a lot of modules that you can use or you can start from scratch and work your way up.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
Easiest example of an web server written in node.

Related

Run two app on same server or on same port

I have MEAN stack apps, I want a suggestion about to run both them on the same server or on the same port(suggestion which one is better).I found article about to run server on the same port using proxies,also I want to know is this a good idea.There is some troubles about running on same server,because I'm using Angular 6 as Front-end, I have to build those files into my node's (which is one in this case) public folder,so I think that's impossible (I think), because there must be index.html and it must be one and not only index.html, other files too. And if this two cases is not good deal.please just let me know about that.Thanks for help and thanks for attention.

Node.js server automatically start

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?
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 .
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.
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.
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:
Several node processes load balanced behind a web server (nginx most commonly used)
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

Do i need to do any settings on node.js app running on Google app engine?

Here's the deal, I want to use Google App Engine, because of its easy to use and scalability is important, due to google infra scalability I decided to give it a try over Amazon Web Service, which will take quite some times to learn and deploy. But I have few doubts on using node.js on Google App engine. For example
1) Do i need to do manually clustering on each node.js server or App engine will do it by itself? (To improve each server performance)
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
}
2) Do i need to care about Load balancing on multiple node.js servers?
3) Do i need to reverse proxy the webserver to so that it could serves the static files even faster? (node.js has been known to poorly serves the static files)
4) How do i run redis on App engine?
5) Do i need to use Channel API or Socket API to make my node.js application to be realtime? (this app has a simple live update, push notifications, chat between users)
6) App Engine is cheaper or EC2 in the long run?
I know I ask alot of questions, but these are the questions that Im struggling to clarify by myself. Hopefully someone with a better experience could explain this to me.
Regards,
Jack Moscovi
First of all this isn't the right way to ask a question. Please go through FAQs, I am answering this still to help you. But from next time please google your queries first.
Do i need to do manually clustering on each node.js server or App engine will do it by itself? (To improve each server performance)
Google's App Engine distribution channel works on its own and hence it has nothing to do with your manual clustering and all.
Do I need to care about Load balancing on multiple node.js servers?
Addition and removal of instances is done automatically and over them a tier of load balance mechanism is there, so you need not to worry.
Do I need to reverse proxy the webserver to so that it could serves the static files even faster? (node.js has been known to poorly serves the static files)
The choice is yours, for general static content I personally just rely on the file system without any explicit reverse proxy method because hey in the end google's app engine is doing fair bit of that. Run a test with both mechanism and based on results choose what you need to do.
For dynamically loading images, I suggest that you use separate container etc.
How do I run redis on App engine?
You don't, app engine is for programming environment why would you want to use it for storage purposes. Use a compute engine and install redis or you can deploy a bitnami or google's infrastructure directly.
Do i need to use Channel API or Socket API to make my node.js application to be realtime? (this app has a simple live update, push notifications, chat between users)
This is not related to your cloud architecture. You need to do what is required at your application level. Having said that Socket.io is pretty famous.
App Engine is cheaper or EC2 in the long run?
The price distribution is given for both, just compare them.
You can run your Node.js app in App Engine (Flex) and App Engine takes care of automatically scaling your Node.js app and load balancing as well. I don't think you need to create a cluster or worry about load balancing yourself.
You can run Redis on a Compute Engine instance in Google Cloud. Here's a page that describes how:
https://cloud.google.com/nodejs/resources/databases/redis

Is it possible to monitor / retrieve variable values in use on NodeJS server?

What tools can you use to monitor or access data in use on a Node server? A comparison would be using the Javascript Console for a browser and being able to console.log a variable value to the console or execute methods. Is there something similar you can do with a NodeJS server?
Edit: An example; if I deployed my server to an AWS ec2 instance could someone managing the datacenter theoretially access my server and monitor/view the data in use? If so, how?
I think this will help you in debugging
http://techblog.netflix.com/2015/12/debugging-nodejs-in-production.html

If Node.js is server side then is it not visible to the client?

I undesrand that Node.js is a server side (implementation?) of JavaScript. Server side means executes on the server (like say PHP or Python). Does that mean the code you write in JavaScript in Node is "invisible" (to the client)? I'm not quiet keen on server side stuff and this subject interests me. So say, you write something really super simple such as console.log("Hello World"); then that gets executed on the server and doesn't get shown to the client (like View Source, etc.)? Am I right?
I'm asking this here to seek an easier (small) explanation of the idea. Also, is this possibly something I'm looking for?
Yes, Node.js code runs completely on the server (just like python). In your link the goal is to encrypt the source code because a client has access to the servers filesystem. To communicate with the client you will need another component like the http module.

Resources