Node.js is a single-threaded runtime environment as compared to other server side technologies. This is only to avoid blocking I/O and provide support for asynchronous I/O. Under the hood libuv handles threading, file system events, implements the event loop, timer functions, features thread pooling etc. This is important to understand and digest since it is the working of Node.js as a runtime.
Getting Started with Node.js
Once you install it, you can use a very a command line application or terminal to interact with:
node --version
v8.11.1
This verifies that you have Node.js installed and is now up and running. To get started with Node.js let us try its terminal version. Start by typing:
node
In command line, this is a perfect way to access JavaScript. Now you will be notice that the prompt is different `>`, so type
> console.log('Hello, World!');
And as soon as you press enter, it will return the following result:
Hello, World!
undefined
Create a Hello World HTTP Server
Let us take create a mini Node.js server application. Create an empty directory, and inside it create a new file with name server.js. Inside server.js you will create your first Node.js server. Open it and type the following:
const http = require('http');
const server = http
.createServer((request, response) => {
// http header + MIME type
response.writeHead(200, { 'Content-Type': 'text/plain' });
// send a response to the client
response.write('Hello World!');
// end the response
response.end();
})
.listen(8080, () => {
console.log('Server Listening on port 8080');
}); //the server object listens on port 8080
The request object knows the information about the incoming request from the client. It can be used to represent URL or any HTTP header coming from the client. The response object is what we are using to send information back to the client. In the above example, notice the first line in the callback function. We are using a response.writeHead() method to send an HTTP status code along with a header that indicates a MIME type.
Different HTTP status code exists for a reason and all have their own meaning and usage. For example, in our case, we are using 200 which indicates that the response from the server to the client is a success. Another example would be status code 404 means that the resource a client is requesting from the server does not exist.
Along with the correct MIME type and an HTTP status code, a server often responds to a client’s request with a response. In Node.js, this is done by response.write() method. Next, in the callback function, we need a way to end the current request of a client (or multiple clients) from the server. This done by calling response.end() method. This method is always called in the end and thus, indicates that the response is sent from the server to the client.
Lastly, createServer() method provides us a way to define a port number on which the server listens to incoming requests. The callback provided inside it is optional.
To see this in action, traverse to the same directory in your terminal and type:
```shell
node server.js
Server Listening on port 8080
```