Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

\n not breaking and creating a new line

Andrews recommended code is

res.write(new Date() + "\n");

the date prints, but the line doesn't break like it does in his example. I am using node.js on windows if that matters..... =

1 Answer

Neil McPartlin
Neil McPartlin
14,662 Points

Hi Britin. I think you are referring to this video...

https://teamtreehouse.com/library/build-a-simple-dynamic-site-with-nodejs/creating-a-simple-server-in-nodejs/creating-a-simple-server

I'm also working with nodejs locally on my Windows 7 machine, and the code provided by Andrew seems to work for me. Here's my full app.js and for good measure I added a 2nd \n to create an additional new empty line...

app.js

var http = require('http');
http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  setInterval(function(){
    response.write(new Date() + "\n\n");
  }, 1000);
  //response.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://<workspace-url>/');

Output to browser

Fri Apr 06 2018 12:45:04 GMT+0200 (Central Europe Daylight Time)

Fri Apr 06 2018 12:45:05 GMT+0200 (Central Europe Daylight Time)

Fri Apr 06 2018 12:45:06 GMT+0200 (Central Europe Daylight Time)

Fri Apr 06 2018 12:45:07 GMT+0200 (Central Europe Daylight Time)

Fri Apr 06 2018 12:45:08 GMT+0200 (Central Europe Daylight Time)

Fri Apr 06 2018 12:45:09 GMT+0200 (Central Europe Daylight Time)

Fri Apr 06 2018 12:45:10 GMT+0200 (Central Europe Daylight Time)

Fri Apr 06 2018 12:45:11 GMT+0200 (Central Europe Daylight Time)

Fri Apr 06 2018 12:45:12 GMT+0200 (Central Europe Daylight Time)

Fri Apr 06 2018 12:45:13 GMT+0200 (Central Europe Daylight Time)

Fri Apr 06 2018 12:45:14 GMT+0200 (Central Europe Daylight Time)

Fri Apr 06 2018 12:45:15 GMT+0200 (Central Europe Daylight Time)

If nothing in here helps, I'm happy to try with your app.js.

Thank you! I'm not really sure where my error was because now it works fine.