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 Build a Simple Dynamic Site with Node.js Creating a Simple Server in Node.js Creating a Simple Server

How to make node.js works on my localhost without the launch workspace?

the Launch workspace seems fine, however I am trying to make the localhost by using the Apache on my own HTTP server and web browser. I installed node software on my laptop and open the git bash and type "node app.js" and running the web server as console.log('Server running at http://<workspace-url>/'); When I am open the file app.js on my localhost file. I only can see the codes and its not showing the javascript console or anything. I have tried localhost:3000 and it said could not find the page. I am not sure how it work on my localhost.

Do I have to updated to my ip address 127.0.0.1 and port 3000 leave it alone?

var http = require('http');
http.createServer(function (request, response) {
  homeRoute(request, response);
}).listen(3000, '127.0.0.1');
console.log('Server running at http://<workspace-url>/');

2 Answers

Hi Brian,

You are creating a webserver with that node.js code so you don't need to supply another webserver yourself like apache. I would make sure that apache isn't running in case it causes any conflicts.

When typing the command "node app.js" you want to make sure that you are in the same directory as where your "app.js" file is located. Use the "cd" command to change your directory if needed.

After running the command you should see your console.log statement right below where you typed "node app.js"

Then you would type "localhost:3000" in your browser address bar and you should then see whatever response you currently have set in your code.

Once you have this working correctly you only need to go back to your command prompt when you've made changes to your js files. You'll need to stop node from running with ctrl - c and then run "node app.js" to restart it again. Then you can refresh your browser and you'll see the new changes you've made in your code.

Let me know if you're still stuck on any part of this and need more clarification.

michael7williams
michael7williams
9,638 Points

Thanks Jason. I'm too used to WAMP for localhost. I derped hard and forgot that THIS IS A LOCAL WEB SERVER.. hah...

First, you would want to know which process is using port 3000, you can do that using this command in terminal:

sudo lsof -i :3000

This will list all PID listening on this port, once you have the PID you can terminate it with the following:

kill -9 {PID}

Note: replace {PID} with the port number shown in terminal after the first command ('lsof').