Introduction:
Node.js is a server-side platform built on Google Chrome's JavaScript Engine, which is an open source, cross-platform runtime environment for developing server-side and networking applications.
Node.js became really, really popular in the last couple of years.
If you think about it, many developers out there knows JavaScript, so thinking in terms of coding Client Side and Server Side, Node.js really allows Developers to develop client-side and server-side applications with one single language.
Node.js make use of a Single Threaded and Asynchronous architecture, which essentially means a large number of concurrent connections with high throughput on a single thread. Not only does Node.js applications deliver high performance and scalability but the fact that these applications are lightweight makes Node.js a great runtime environment for your applications.
Who uses Node.js?
Companies such as New York Times, Netflix, Uber and LinkedIn uses Node.js, more info on this can be found here
Lately, I've seen a lot of people using Node.js for RESTful API's, Chatbot's, Data Streaming Applications, MEAN Stacks, and hey.. this blog runs on Node.js :D
Getting Started:
We will setup our environment, create a Hello World application that just prints out to the console, and then following a Simple HTTP Server.
Setup Node.js:
For CentOS/RHEL Operating Systems:
$ sudo curl --silent --location https://rpm.nodesource.com/setup_7.x | bash -
$ sudo yum install nodejs -y
``` <p>
For Debian/Ubuntu Operating Systems:
```language-bash
$ curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
$ sudo apt-get install -y nodejs
``` <p>
For MacOSX:
```language-bash
brew install nodejs
``` <p>
**Running a Hello World App:**
As the tradition goes, we need to make our application print Hello, World. In order to do this, create a file called `hello.js`:
```language-javascript
console.log("Hello, World!")
``` <p>
Now that we have the above contents in our file, execute the file:
```language-bash
$ node hello.js
Hello, World!
``` <p>
**Simple HTTP Server:**
Now, for our HTTP Server, which will be similar to Apache HTTP or Nginx Server. We will import our required modules, create the servicer which will listen to the clients requests, read the request and return the response, which in this case will respond with "Request Response: OK"
Our file: `app.js`:
```language-javascript
var http = require("http");
http.createServer(function (request, response) {
// Sends the HTTP header with 200 Status and
// text/plain Content Type
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Request Response: OK"
response.end('Request Response: OK\n');
}).listen(8080);
// Console will print the message
console.log('Server Running at http://127.0.0.1:8080/');
``` <p>
Run the server:
```language-bash
$ node app.js
Server Running at http://127.0.0.1:8080/
``` <p>
Make a request to the given address and you should then be returned with the response "Request Response: OK"
**Final Thoughts:**
This was a basic introduction to Node.js, I will write more tutorials on Node.js in the future that will be found under the [Nodejs](https://sysadmins.co.za/tag/nodejs/) tag.
Have a look at these awesome nodejs projects:
* https://github.com/sqreen/awesome-nodejs-projects
Comments