express send raw html
To send raw HTML using Express, you can follow these steps:
Import the necessary modules:
const express = require('express'); const app = express();
Create a route handler for the desired endpoint:
`` app.get('/html', (req, res) => { // Your HTML code here const html =
Raw HTML Example Hello, World!
`;res.send(html); }); ```
Start the server:
app.listen(3000, () => { console.log('Server running on port 3000'); });
Explanation:
In the first step, we import the Express module and create an instance of the app.
Next, we define a route handler using the
app.get()
method. This handler listens for GET requests to the/html
endpoint. Inside the handler, we create a variablehtml
that stores the raw HTML code you want to send as a response.Finally, we start the server by calling the
app.listen()
method. This tells Express to listen for incoming requests on port 3000. Once the server is running, it logs a message to the console.
By following these steps, you can send raw HTML as a response using Express. Remember to customize the HTML code inside the html
variable to suit your specific needs.