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 trialSaud Tauqeer
Courses Plus Student 8,099 PointsSomeone 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
231,172 PointsIt 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.
Saud Tauqeer
Courses Plus Student 8,099 PointsSo i should just keep following the course for now?
Steven Parker
231,172 PointsUnless you have a specific question about something that is holding you up.
Saud Tauqeer
Courses Plus Student 8,099 PointsThe 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
231,172 PointsWhat time index in the video is this said?
I remember completing this course using only the workspace.
Saud Tauqeer
Courses Plus Student 8,099 Pointsso 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
11,580 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
11,580 PointsI 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).
Saud Tauqeer
Courses Plus Student 8,099 PointsThanks 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.
Saud Tauqeer
Courses Plus Student 8,099 PointsSaud Tauqeer
Courses Plus Student 8,099 PointsSteven Parker