Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Build a Simple Dynamic Site with Node.js Creating a Simple Server in Node.js Creating a Simple Server

Someone help me setting up node server locally

I have no prior experience so can someone help me out so i can freely experiment with dynamic websites.. ill be using them eventually so i think it will be good for me if i start doing practice locally too.

Steven Parker
Steven Parker
216,012 Points

It looks like you're taking the right course. It should guide you through setting up a server. If you still need some help after (or during) the course, you can try asking a specific question.

So i should just keep following the course for now?

Steven Parker
Steven Parker
216,012 Points

Unless you have a specific question about something that is holding you up.

The only thing holding me is as the teacher said “u will need to locally make a node.js sv to view the dynamic html” so ill just be using stuff on a terminal how will i know the site im making is working as it should be.. thats why i wanted to make a node.js sv locally and then work on forward with the project files

Steven Parker
Steven Parker
216,012 Points

What time index in the video is this said?

I remember completing this course using only the workspace.

so i installed node and im trying to make a new folder and add a file.js to it and then work on it on my default browser. how can i do that? or is there a better way to write node locally. cause so far i have been only using workspaces.

http://prntscr.com/kdsosi i saw this on youtube and followed it on my machine. .code is supposed to open the file created on your default editor but it gives this error. sorry im like so new to all this... cant even add files to work on node Steven Parker I have installed node and npm. Its functional too. How do i make an app.js file and start working?

1 Answer

Rebecca Jensen
Rebecca Jensen
11,568 Points

"and im trying to make a new folder and add a file.js to it and then work on it on my default browser."...

I recommend downloading the starter files if you can, though it would be fine to start with a simple app.js file.

"How do i make an app.js file and start working?"

You need npm not just installed generally, but to initialize it. To do that, run the command npm init -y in your project folder. This will create a file called package.json.

See more documentation on package.json here: https://docs.npmjs.com/getting-started/using-a-package.json

Once you've set up a simple server in app.js... See documentation: https://nodejs.org/en/docs/guides/getting-started-guide/

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\n');
});

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

Then you can run node name_of_file (name of file = app.js) and you'll see your console.log message in your terminal.

All that said, I believe there's a course about using Node with Express, which is a pretty common way to set up servers these days, so I recommend checking that out.

Rebecca Jensen
Rebecca Jensen
11,568 Points

I also recommend using nodemon so you can run your server and have it update automagically whenever you make changes (instead of having to restart it after each change).

https://github.com/remy/nodemon

Thanks a lot rebecca. i acutally figured it out on my own, Simply installed node.js and npm from node website. then used vs terminal to work with node and made a new folder and file in vs code. really a simple way tho to get started imo.