How to host node.js application on linux cPanel server ?
Introduction on Node Js
Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser. Node.js (Node) is an open source development platform for executing JavaScript code server-side. Node is useful for developing applications that require a persistent connection from the browser to the server and is often used for real-time applications such as chat, news feeds and web push notifications.
Node.js is intended to run on a dedicated HTTP server and to employ a single thread with one process at a time. Node.js applications are event-based and run asynchronously. Code built on the Node platform does not follow the traditional model of receive, process, send, wait, receive. Instead, Node processes incoming requests in a constant event stack and sends small requests one after the other without waiting for responses.
One of the major advantages of Node.js, is that it does not block input/output (I/O). Some developers are highly critical of Node.js and point out that if a single process requires a significant number of CPU cycles, the application will block and that the blocking can crash the application. Proponents of the Node.js model claim that CPU processing time is less of a concern because of the high number of small processes that Node code is based on.
Why should you use node JS?
A common task for a web server can be to open a file on the server and return the content to the client.
Here is how PHP or ASP handles a file request:
1. Sends the task to the computer’s file system2. Waits while the file system opens and reads the file3. Returns the content to the client4. Ready to handle the next request
Here is how Node.js handles a file request:
1. Sends the task to the computer’s file system.2. Ready to handle the next request.3. When the file system has opened and read the file, the server returns the content to the client.4. Node.js eliminates the waiting, and simply continues with the next request.
Node.js runs single-threaded, non-blocking, asynchronously programming, which is very memory efficient.
When to use Node.JS
1. If your server side code requires very few cpu cycles. In other world you are doing non blocking operation and does not have heavy algorithm/Job which consumes lots of CPU cycles.2. If you are from Javascript back ground and comfortable in writing Single Threaded code just like client side JS.
What Can Node.js Do?
1. Node.js can generate dynamic page content2. Node.js can create, open, read, write, delete, and close files on the server3. Node.js can collect form data4. Node.js can add, delete, modify data in your database
What is a Node.js File?
1. Node.js files contain tasks that will be executed on certain events2. A typical event is someone trying to access a port on the server3. Node.js files must be initiated on the server before having any effect4. Node.js files have extension “.js”
Sample Node.js code
Code to display “Hello World” in a web browser. Create a Node.js file named “test.js”, and add the following code:
test.js——–var http = require(‘http’);
http.createServer(function (req, res) { res.writeHead(200, {‘Content-Type’: ‘text/html’}); res.end(‘Hello World!’);}).listen(8080);——–
Save the file as test.js. The code tells the server to write “Hello World!” if anyone (e.g. a web browser) tries to access your computer on port 8080.
How to Run Node.js code ?
In linux server, please go to SSH access. Access to the path of the application where it present.
=======
node test.js
=======
If anyone tries to access your server on port 8080, they will get a “Hello World!” message in return!
Start your internet browser, and type in the address: http://localhost:8080 or http://x.x.x.x:8080