Express HTML and JSON Responses

Aim

To send HTML and JSON responses using Express.js.

HTML Response

Express can send HTML content using the res.send() method.

JSON Response

Express can send JSON data using the res.json() method.

Example

app.get("/", (req, res) => {
    res.send("<h1>Hello from Express!</h1>");
});

app.get("/json", (req, res) => {
    res.json({
        name: "Madhu",
        course: "Node.js",
        topic: "Express.js"
    });
});
    

Output

HTML and JSON responses are successfully demonstrated.