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:
<code class="sh">npm install express</code>
2. Create a Node.js application
Create a new Node.js file, for example index.js:
<code class="javascript">const express = require('express'); const app = express();</code>
3. Configure HTML file
Create an HTML file in your project directory, such as index.html:
<code class="html"><h1>Hello World!</h1></code>
4. Setting Express.js routing
In the index.js file, set up a route to respond to HTML file requests:
<code class="javascript">app.get('/', (req, res) => { res.sendFile(__dirname + '/index.html'); });</code>
5. Start the application
Start the Node.js application using the following command:
<code class="sh">node index.js</code>
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!