The steps to run HTML in Node.js are as follows: Install Express.js. Create a Node.js application. Configure HTML files. Set up Express.js routing. Start the application. Browse the HTML file.
How to run HTML in Node.js
In order to run HTML in Node.js, you can use Following steps:
1. Install Express.js
Express.js is a framework for building web applications. Install it using the following command:
npm install express
2. Create a Node.js application
Create a new Node.js file, for example index.js:
const express = require('express'); const app = express();
3. Configure HTML file
Create an HTML file in your project directory, such as index.html:
<h1>Hello World!</h1>
4. Setting Express.js routing
In the index.js file, set up a route to respond to HTML file requests:
app.get('/', (req, res) => { res.sendFile(__dirname + '/index.html'); });
5. Start the application
Start the Node.js application using the following command:
node index.js
6. Browse the HTML file
Navigate to http://localhost:3000 in your browser (default port), you should see "Hello World!"
The above is the detailed content of How to run html in nodejs. For more information, please follow other related articles on the PHP Chinese website!