Search code examples
node.jsexpressresponsenewlineline-breaks

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


I'm trying to send a string with a newline using Express.

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

persons = [//...];

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

I've read online that as of ES6, backticks can be used to construct multi-lines but it does not seem to be working.

My desired output is:

Phonebook has info for 4 people.

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

I've also tried the following:

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

I've read online that you can also just use '\n' but that also does not work.

What am I doing wrong? I've been following the advice I've found online but I cannot get a new line to appear.


Solution

  • The solution was to use a <br/> tag instead of \n since the purpose of my res.send() was to send HTML to my local browser. Thanks to @Jason.