expressjsHow do I use Express.js to send HTML?
Express.js is a web application framework for Node.js. It is used to create web applications and APIs. It can be used to serve HTML pages to the client.
Here is an example of how to use Express.js to send HTML:
// Require the Express module
const express = require('express');
// Create a new Express application
const app = express();
// Serve an HTML page
app.get('/', (req, res) => {
res.send(`
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
`);
});
// Start the server
app.listen(3000, () => {
console.log('Server started on port 3000');
});
The output of the above code will be:
Server started on port 3000
This code does the following:
- Requires the Express module
- Creates a new Express application
- Serves an HTML page with a
GETrequest - Starts the server on port 3000
Helpful links
More of Expressjs
- How can I use the x-forwarded-for header in Express.js?
- How do I use Yarn to add Express.js to my project?
- How do I find Express.js tutorials on YouTube?
- How can I use Express.js and Nest.js together to create a web application?
- How do I use an Express.js logger?
- How do I set up a YAML configuration file for a Node.js Express application?
- How can I use Express.js to make an XHR request?
- How can I use Express.js to develop a web application?
- What is Express.js and how is it used for software development?
- What are the pros and cons of using Express.js vs Django according to Reddit users?
See more codes...