3

I would like it to be like:

I<3cookies 127.0.0.2

and milk

o_O

var express = require('express');
var app = express();
host = '127.0.0.2'; // Choose a different loopback address;
port = "5555"

app.get('/', function (req, res) {
    res.send("I<3cookies " + host + "\r\n with milk" + "\r\no_O");
});

app.listen(port, host, function () {
    console.log("Listening in on: " + host + port);
});

So far when I run it, it looks like: I<3cookies 127.0.0.1 with milk o_O

Salomon Zhang
  • 1,553
  • 3
  • 23
  • 41
BayCode
  • 93
  • 11
  • 3
    How are you viewing the response? If you're using a browser, the response could be rendered as HTML text content, which doesn't normally leave newlines as-is – [Why does the browser renders a newline as space?](https://stackoverflow.com/questions/588356/why-does-the-browser-renders-a-newline-as-space) Calling [`res.type('text');`](http://expressjs.com/en/4x/api.html#res.type) before `.send()` might help. – Jonathan Lonowski Oct 15 '18 at 01:02
  • This does indeed work - when I run your code, and view the response with `curl 127.0.0.1:5555`, I get the output you expect. If you look in the browser's inspect element panel, you can see that the newlines are there, but the browser doesn't render them. You can verify further with `document.querySelector('body').innerHTML` in the browser's console to see it printed *with* the newlines. – David Oct 15 '18 at 01:16
  • @Jonathan Lonowski the res.type('text'); worked perfectly.Thanks a lot! – BayCode Oct 15 '18 at 02:00

1 Answers1

1
res.send(`I<3cookies ${host}

with milk

o_O`);

Test in Chrome console

> host = '127.0.0.1'
< "127.0.0.1"
> "I<3cookies " + host + [^\r\n] + "with milk" + [^\r\n] + "no_O"
< VM5323:1 Uncaught SyntaxError: Unexpected token ^
> `I<3cookies ${host}

with milk

o_O`
< "I<3cookies 127.0.0.1

with milk

o_O"
Piterden
  • 771
  • 5
  • 17