Node.js is a powerful JavaScript runtime that allows you to build server-side applications with ease. In this tutorial, we’ll walk through the process of creating a basic Node.js application and setting up a simple web server that can serve content to your users. By the end of this guide, you’ll have a working web server that can display a message in the browser.
Install Node.js
Before you can start building your Node.js application, you need to have Node.js installed on your system. Follow these steps to install Node.js:
Download Node.js:
Visit the Node.js website and download the LTS (Long-Term Support) version for your operating system.
Install Node.js:
Run the installer and follow the on-screen instructions to complete the installation.
To verify the installation, open your terminal (Command Prompt, PowerShell, or terminal on macOS/Linux) and type: node -v
Create Your Node.js Project
Create a Project Directory: Open your terminal and create a new directory for your project.
mkdir my-node-app
cd my-node-app
Initialize a Node.js Project:
Inside your project directory, initialize a new Node.js project using npm (Node Package Manager). This will create a package.json file where project dependencies and scripts are managed.
Run the command: npm init -y
The -y flag automatically accepts the default settings for the project. If you want to customize these settings, simply run npm init without the -y flag.
Create a Basic Web Server
Now that your Node.js project is set up, you can create a basic web server to serve content.
Create the Server File:
In your project directory, create a new file named server.js
:
Write the Web Server Code:
Open the server.js
file in your favorite text editor and add the following code:
// Load the http module to create an HTTP server
const http = require('http');
// Define the hostname and port
const hostname = '127.0.0.1';
const port = 3000;
// Create the server and define the response for incoming requests
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, world!\n');
});
// Start the server and listen on the specified port and hostname
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
http
: This is a built-in Node.js module that allows you to create an HTTP server.hostname
: The hostname where the server will run, typically localhost
or 127.0.0.1
for development purposes.port
: The port number the server will listen on (3000 is commonly used for development).http.createServer()
: This function creates an HTTP server that listens for requests and sends a response.
The server.listen()
method starts the server and makes it listen on the specified hostname and port.
Run Your Node.js Web Server
In the terminal, in the same directory as server.js run the following command: node server.js
Open your web browser and navigate to http://127.0.0.1:3000/
. You should see the message “Hello, world!” displayed in your browser, if that’s working then you’ve just created your first Node.js webserver!
Without going into too much detail, you can basically serve normal webpages instead of plain text, to do this you can modify the above code with these parts below, setting the content-type to html and outputting HTML content.
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Node.js App</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>Welcome to my Node.js web server.</p>
</body>
</html>
`);
From here you can customize the application to do whatever you want, you could load the contents of a .html file and serve this dynamically depending on the URI, or create your own API endpoint for clients to communicate with.
Hopefully this has been a very basic example of a Node.js application for you.