使用 Express 和 node.js 在响应中发送换行符

Sending a newline in a response using Express and node.js

我正在尝试使用 Express 发送带有换行符的字符串。

const express = require('express');
const app = express();

persons = [//...];

app.get('/info', (req,res) => {
    res.send(`Phonebook has info for ${persons.length} people.
    ${Date()}`);
});

我在网上看到,从 ES6 开始,反引号可以用来构造多行,但它似乎不起作用。

我想要的输出是:

Phonebook has info for 4 people.

Thu Oct 10 2019 18:54:01 GMT-0700 (Pacific Daylight Time)

我也试过以下方法:

app.get('/info', (req,res) => {
    res.send(`Phonebook has info for ${persons.length} people.\n${Date()}`);
});

我在网上看到您也可以只使用 '\n' 但这也行不通。

我做错了什么?我一直在遵循我在网上找到的建议,但我无法让新行出现。

解决方案是使用 <br/> 标记而不是 \n,因为我的 res.send() 的目的是将 HTML 发送到我的本地浏览器。感谢@Jason。

作为替代方案,您可以使用 HTML 元素分隔两个句子,例如

app.get('/info', (req,res) => {
    res.send(`<p>Phonebook has info for ${persons.length} people.</p><p>${new Date()}</p>`);
});

这应该给你这样的输出:

Phonebook has info for 4 people.
Sat Jan 09 2021 18:34:35 GMT+0000 (Greenwich Mean Time)