Create a Web Server using Node.js to Serve Simple HTML (2024)

Create a Web Server using Node.js to Serve Simple HTML (3)

What is Node.js?

  • Node.js is an open source web server environment
  • It can run JavaScript outside the browser.

In this post, let’s write a web server using Node.js to serve simple HTML files.

Requirement: Install Node.js

LTS version is recommended.

Create a Web Server using Node.js to Serve Simple HTML (4)

Step 1: Create index.html to be served

Create a Web Server using Node.js to Serve Simple HTML (5)

Step 2: write server code using http module

Create a Web Server using Node.js to Serve Simple HTML (6)

Step 3: run the server

$ node server.js

Then your index.html page will be available on URL: http://localhost:8080

Create a Web Server using Node.js to Serve Simple HTML (7)

Our Node server can serve different HTML content based on different HTTP requests. So we can write our server like this:

Create a Web Server using Node.js to Serve Simple HTML (8)

And here are results:

Create a Web Server using Node.js to Serve Simple HTML (9)
Create a Web Server using Node.js to Serve Simple HTML (2024)

FAQs

How to serve HTML with Node js? ›

Simple steps to serve a static HTML file
  1. npm i nodemon.
  2. npm install express.
  3. const express = require('express'); const path = require('path'); const app = express(); ...
  4. app.use(express.static(path.join(__dirname, 'public'))); ​ ...
  5. app.get('/', async(req, res) => { res.sendFile(path.join(__dirname, 'public', 'home.html')); });

How to create a simple HTTP server in Node js? ›

// Step 1: Import the HTTP module const http = require('http'); // Step 2: Create the server using the http. createServer() method const server = http. createServer((req, res) => { // Step 3: Set the response headers res. writeHead(200, {'Content-Type': 'text/plain'}); // Step 4: Send the response body res.

Can Node js be used as a Web server? ›

Node. js provides capabilities to create your own web server which will handle HTTP requests asynchronously. You can use IIS or Apache to run Node. js web application but it is recommended to use Node.

How to make HTML in Node js? ›

In the beginning, you fetch the content of the HTML file (1). Then you insert it into the code of a very simple JavaScript module that wraps HTML into a Hogan template (2). The code prepared in this way is then compiled using module. _compile (3).

How to set up a Node.js web server? ›

  1. What is Node. js? ...
  2. Install Node and npm. First, you have to download Node and npm from these links.
  3. Get package. json file. ...
  4. Install dependencies. Next, you have to install express which is the Node. ...
  5. Write a script. Write these two scripts in your package. ...
  6. Create a port. Now in the end, type this code in your server.js file.
Oct 12, 2022

How to start a simple HTTP server using npm? ›

Installing HTTP server using npm

Run the command line/terminal on your system (it doesn't matter which directory you're currently in). Execute npm install -g http-server . Once npm finishes, you have the tiny HTTP-server installed. That's it.

How do I host a simple HTTP server? ›

You just need to type a single line command in your terminal to implement this HTTP server. Thats just it, fire up your browser and the present directory files can be seen on http://localhost:8000. If the directory has a files name such as index. html, then that file will served as the initial file.

Is node a good web server? ›

Node really shines in building fast, scalable network applications. This is due to its capability of handling a huge number of simultaneous connections with high throughput.

What is the difference between node JS server and web server? ›

js is an open-source server environment. Node. js uses JavaScript on the server. The task of a web server is to open a file on the server and return the content to the client.

How do I start a simple node server? ›

Step 1: Create a Simple Node. js Server
  1. Initialize a Node. js Project: Create a new directory for your project and run npm init in your terminal to create a package. json file.
  2. Create a Server File: Create a file named server. js .
  3. Write the Server Code: In server.js , write the following code:
Feb 5, 2024

What is the difference between application server and web server? ›

Web servers deliver static content, like HTML pages, images, videos, and files. Application servers deliver dynamic content, like real-time updates, personalized information, and customer support. Does not typically use multithreading. Uses multithreading to process requests concurrently.

How to render HTML file in Express js? ›

How to render HTML file in Express JS
  1. Using the Built-in Templating Engine (Example: EJS) ...
  2. Set up your Express application and configure the templating engine (EJS in this case): const express = require('express'); ...
  3. Create an index.ejs file in the "views" directory: <!
Jul 23, 2023

What is a program that serves requested HTML files and page? ›

A Web server is a program that uses HTTP (Hypertext Transfer Protocol) to serve the files that form Web pages to users, in response to their requests, which are forwarded by their computers' HTTP clients.

Is Node a good web server? ›

Node really shines in building fast, scalable network applications. This is due to its capability of handling a huge number of simultaneous connections with high throughput.

Top Articles
Latest Posts
Article information

Author: Allyn Kozey

Last Updated:

Views: 6020

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Allyn Kozey

Birthday: 1993-12-21

Address: Suite 454 40343 Larson Union, Port Melia, TX 16164

Phone: +2456904400762

Job: Investor Administrator

Hobby: Sketching, Puzzles, Pet, Mountaineering, Skydiving, Dowsing, Sports

Introduction: My name is Allyn Kozey, I am a outstanding, colorful, adventurous, encouraging, zealous, tender, helpful person who loves writing and wants to share my knowledge and understanding with you.