MahmutTariq
MahmutTariq

Reputation: 237

res.send() gives HTML code instead of rendering it

I'm using Express 4 with Node JS, below is my code:

const express = require('express');

const router = express.Router();

router.get('/socket', (req, res, next) => {
    res.send('<p>Hello</p>');
});

But it shows the following

enter image description here

instead of rending the p tag, what am I doing wrong?

Upvotes: 6

Views: 12325

Answers (3)

HITESH CHOUDHARY
HITESH CHOUDHARY

Reputation: 5

To get h1 header in output, you can use the following code here i have used 3 const to demonstrate their use with it

const express = require('express');

const router = express.Router();

router.get('/socket', (req, res, next) => {
    const city="Londan";
    const country="England";
    const temp=24;
    res.send("<h1>Hello, The temperature in "+city+","+country+" is "+temp+"</h1>");
}); 

use " nodemon app.js " to run server " ctrl + c " to end a server.

Upvotes: -2

Nuriddin Kudratov
Nuriddin Kudratov

Reputation: 518

res.send(). Sends a string response in a format Try this

> res.write('<h1>Hello, World!</h1>');

res.send can only be called once in a code, but it is same as res.write + res.end()

Upvotes: 2

pr0p
pr0p

Reputation: 2358

res.setHeader('Content-type','text/html')

set the header before sending response.

Upvotes: 15

Related Questions