Node.js res.send()提供HTML代码,而不是呈现它

Node.js res.send()提供HTML代码,而不是呈现它,node.js,express,Node.js,Express,我将Express 4与Node JS一起使用,下面是我的代码: const express = require('express'); const router = express.Router(); router.get('/socket', (req, res, next) => { res.send('<p>Hello</p>'); }); const express=require('express'); const router=expre

我将Express 4与Node JS一起使用,下面是我的代码:

const express = require('express');

const router = express.Router();

router.get('/socket', (req, res, next) => {
    res.send('<p>Hello</p>');
});
const express=require('express');
const router=express.router();
路由器.get('/socket',(req,res,next)=>{
res.send(“Hello

”); });
但它显示了以下内容

我没有撕开p标签,我做错了什么

res.setHeader('Content-type','text/html')
在发送响应之前设置标头

在发送响应之前设置标题。

res.send()。
以格式发送字符串响应 试试这个

>res.write('Hello,World!');
res.send
在一个代码中只能调用一次,但它与
res.write
+
res.end()
res.send()。
以一种格式发送字符串响应 试试这个

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

res.send
在一个代码中只能调用一次,但它与
res.write
+
res.end()

要在输出中获取h1头,可以使用以下代码 在这里,我使用了3个常量来演示它们的用法

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>");
}); 
const express=require('express');
const router=express.router();
路由器.get('/socket',(req,res,next)=>{
const city=“Londan”;
const country=“英格兰”;
常数温度=24;
res.send(“您好,“+城市+”、“+国家+”的温度是“+温度+”);
}); 
使用“nodemon app.js”运行服务器
“ctrl+c”结束服务器。

要在输出中获取h1头,可以使用以下代码 在这里,我使用了3个常量来演示它们的用法

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>");
}); 
const express=require('express');
const router=express.router();
路由器.get('/socket',(req,res,next)=>{
const city=“Londan”;
const country=“英格兰”;
常数温度=24;
res.send(“您好,“+城市+”、“+国家+”的温度是“+温度+”);
}); 
使用“nodemon app.js”运行服务器
“ctrl+c”结束服务器。

也许,您并没有告诉浏览器,您发送的是HTML数据,而不是文本file@Lixas我该怎么做?我尝试了res.type('.html')&res.type('html'),但没有不同的结果。可能,您并没有告诉浏览器,您发送的是html数据,而不是文本file@Lixas我该怎么做?我试过res.type('.html')&res.type('html')没有不同的结果您在询问问题时执行相同的操作。它不会呈现html内容,会给出输出您在询问问题时执行相同的操作。它不会呈现html内容,会给出输出