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

Another student who Can't preview the file in browser

I have seen in the discussions that other people are having the same issue as I am but I haven't seen how they managed to fix the problem.

Here is my code.

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((request, response) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

I then go to the console and type out

node app.js

I get exactly as I expect. Server running at http://127.0.0.1:3000/ prints to the console.

Then I press preview button as shown in the video and I get directed to page that says "Workspace Unavailable. This is a preview link for a Treehouse Workspace that is not currently active. If you are the workspace owner, you can launch it again via the Treehouse site."

Any ideas what I am doing wrong?

1 Answer

Simon Coates
Simon Coates
8,223 Points

I got the same error with your code, but it disappeared when I tried:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port,  () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

I eliminated the hostname thing and fixed the parameters. You had response, but were calling res. Hope that helps.