Hello folks! welcome back to a new section of our tutorial on Node.js. In this section of our tutorial on Node.js, we will be creating our first Node.js Application.
Before creating a substantial "Hello, World!" application making use of Node.js, let's see the components of a Node.js application. A Node.js is comprised of the following three important components -
Before creating a substantial "Hello, World!" application making use of Node.js, let's see the components of a Node.js application. A Node.js is comprised of the following three important components -
- Import required modules - We make use of the required directive to load Node.js modules.
- Create Server - A server which will listen to client's requests similar to Apache HTTP Server.
- Read request and return response - The server created in an earlier step will read the HTTP request made by the client which can be a browser or console and return the response.
Creating Node.js Application
Step 1 - Import Required Module
We use the require directive to load the http module & store the returned HTTP instance into an http variable as follows -
var http = require("http");
Step 2 - Create Server
Now, let us utilize the created http instance & call http.createServer() method to create a server instance and then we bind it at port 8081 via the listen method associated with the server instance. Pass it as function with parameter request and response. Write the sample implementation to always return the "Hello World".
http.createServer(function (request, response) { // Send the HTTP header // HTTP Status: 200 : OK // Content Type: text/plain response.writeHead(200, {'Content-Type': 'text/plain'}); // Send the response body as "Hello World" response.end('Hello World\n'); }).listen(8081); // Console will print the message console.log('Server running at http://127.0.0.1:8081/');
The above code is enough to create a HTTP server which listens, i.e., waits for a request over 8081 port on the local machine.
Step 3 - Testing Request and Response
Let's put step one and two together in a file called main.js and start our HTTP server as shown below -
var http = require("http"); http.createServer(function (request, response) { // Send the HTTP header // HTTP Status: 200 : OK // Content Type: text/plain response.writeHead(200, {'Content-Type': 'text/plain'}); // Send the response body as "Hello World" response.end('Hello World\n'); }).listen(8081); // Console will print the message console.log('Server running at http://127.0.0.1:8081/');
Now execute the main.js to start the server as follows -
$ node main.js
Verify that the Output Server has started.
Server running at http://127.0.0.1:8081/
Make a Request to the Node.js Server
Open http://127.0.0.1:8081/ in any browser and observe the following result -
Congratulations! You have your first HTTP server up and running which is responding to all the HTTP requests at port 8081.
READ: Node.js | Introduction
Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we will be studying about Node.js REPL Terminal.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.