Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 307
Brendan Avatar asked Oct 11 '19 02:10

Brendan


People also ask

How do I add a new line in node JS?

Try using \r\n instead of just \n. \n is fine; you're probably viewing the log file in notepad or something else that doesn't render non-Windows newlines. Try opening it in a different viewer/editor (e.g. Wordpad).

Does \n work in JavaScript?

"\n" does work. If you do a document.

What is Express () in node JS?

Express is a node js web application framework that provides broad features for building web and mobile applications. It is used to build a single page, multipage, and hybrid web application. It's a layer built on the top of the Node js that helps manage servers and routes.

How do you send a response in node JS?

Methods to send response from server to client are:Using send() function. Using json() function.

Is it possible to run HTTP and Node JS in same process?

It's a bad practice in node.js to execute such 'complex' tasks in the same process as http server. Consider using background workers, queues or something like that.

What is NodeJS used for in programming?

Nodejs is very powerful as it allows javaScript to be used as a server-side language, which is achieved by the modules/packages it comes with, such as http which allow it to create an HTTP server. With nodeJS, we can also build GUIs, Mobile applications, Desktop applications, APIs, Command Line Tools, etc.

What is the difference between REST API and Node JS?

With nodeJS, we can also build GUIs, Mobile applications, Desktop applications, APIs, Command Line Tools, etc. REST (Representational State Transfer) is an architectural style that defines a set of constraints to be used to create web services. REST API is a way of accessing web services in a single and flexible way without having any processing.

What is a JSON line?

This format is called JSON Lines . Instead of sending: Line-separated data elements can be read, parsed and processed as they are being read from the socket, without waiting for the rest of the elements to be received. This approach is much more memory efficient as only the elements which are processed at a given time are kept in memory.


1 Answers

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.

like image 147
Brendan Avatar answered Oct 14 '22 15:10

Brendan