Hi. In this tutorial, we will create a simple node.js web server and handle HTTP requests. The node.js framework is commonly used to build server-based applications which are then used to show the content to users.
1. Introduction
1.1 Configuring Node.js
Install Node.js on Windows, you will need to download the installer from this connect. Click the installer (also include NPM package manager) for your platform and run the installer to get started with the Node.js setup wizard. Follow the steps of the wizard and click Finish when you are finished. If all goes well, you can go to the command prompt to check if the installation was successful, as shown in Figure 1.
2. Create a Node.js web server
To configure the application, we will need to navigate to a path where our project will reside. To program stuff I use Visual Studio code like my favorite IDE. You are free to choose the IDE of your choice.
2.1 Configure dependencies
Go to the project directory and run npm init -y
to create a package.json
drop off. This drop off contains the relevant metadata for the project and is used to manage project dependencies, script, version, etc. Add the following code to the file where we will specify the required dependencies.
package.json
{ "name": "webserver", "version": "1.0.0", "description": "nodejs and webserver", "main": "index.js", "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "keywords": [ "nodejs", "webserver" ], "author": "c-danatl", "license": "MIT", "dependencies": { "nodemon": "^2.0.9" } }
To download the dependencies, navigate to the path of the directory containing the file and use the npm install
order. If all goes well the dependencies will be loaded into the node_modules
folder and you’re ready to go to the next steps.
2.2 Creating the index controller
To create a controller file which will import the HTTP module using the require(…)
a function. This module will be used to create the web server and specify the callback function with a request and response parameter.
index.js
// importing node.js core module const http = require('http'); // application endpoints - // Index - http://localhost:5001/ const server = http.createServer(function (req, resp) { // handling incoming requests here // setting response header resp.writeHead(200, { 'Content-Type': 'text/html' }); // setting response content resp.write('<html><body><p>Welcome to javacodegeeks</p></body></html>'); resp.end; // todo - similarly you can create other endpoints. }); // start app // listen for any incoming requests const PORT = process.env.PORT || 5001; server.listen(PORT, () => { console.log(`Server started on port ${PORT}`); });
3. Run the application
To run the application, navigate to the project directory and enter the following command as shown in figure 2. If all goes well, the application will be launched successfully on the port number. 5001
.
4. Project demo
When the application is launched, open the browser of your choice and click on the following URL – http://localhost:5001
. The index page will be shown to the user as in Fig. 3.
That’s it for this tutorial and I hope the article has served you with everything you were looking for. Happy learning and don’t forget to share!
5. Summary
In this tutorial, we have learned how to create a node.js web server using simple steps. You can download the source code for this tutorial from Downloads section.
6. Download the project
This was a programming tutorial to create a web server in a node.js application.