Render static html files in express

To render static HTML files in Express, you can follow these steps:

  1. First, make sure you have Express installed in your project. You can do this by running the following command in your terminal:

npm install express

  1. Create a new file called server.js (or any other name you prefer) in the root directory of your project.

  2. In the server.js file, require the Express module and create an instance of the Express application:

javascript const express = require('express'); const app = express();

  1. Next, specify the directory where your static HTML files are located using the express.static middleware. For example, if your HTML files are in a folder called "public", you can add the following line of code:

javascript app.use(express.static('public'));

This will make Express serve any files in the "public" folder as static files.

  1. Finally, start the Express server by listening on a specific port. For example, you can use port 3000:

javascript app.listen(3000, () => { console.log('Server is running on port 3000'); });

This will start the server and print a message in the console indicating that it's running.

That's it! Now, any static HTML files placed in the specified directory will be served by Express when you access the corresponding URL. For example, if you have a file called index.html in the "public" folder, you can access it in your browser by navigating to http://localhost:3000/index.html.