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

Node.js and http.createServer(function (request, response) {

I have the following code that creates a simple web server but do not understand it.

//The server listens on port 3000 so the console log doesnt excute until the port is initiated const server = http.createServer(function (request, response) { console.log("The request ", request.url, request.method); //console.log("The response", response); router.home(request,response); router.user(request, response); }).listen(3000);

console.log(Server running at http://<workspace url>/);

in the home and user routers I use console.log to log out when it executes the router.home and router.user and note:

that the first console.log that appears is the /LK.css which is my css file not the line that says: console.log("The request ", request.url, request.method); This should be the first line executed, but it seems to pick up the request in the html with the link to the file LK.css first.

4 Answers

Steven Parker
Steven Parker
231,269 Points

You didn't include the code that references "LK.css", but it's normal for the HTML code to be loaded first and then the JavaScript parts are executed.

A better way to share code involving multiple modules would be to make a snapshot of your workspace and post the link to it here.

I did not know that I will use it next time.

I wondered if you know a good book on node.js for beginners.

Also is it hard to make a node website live.

I have only up loaded static html files But would not know how you include node.. Lastly is it difficult to process forms via node rather than php for example..

Cheers for advice

Steven Parker
Steven Parker
231,269 Points

I find online learning works much better for me than books, and it (at least potentially) gets updated to reflect current technology.

As for building a website with node, have you taken the Build a Simple Dynamic Site with Node.js course?

Hi thanks for the response I have taken the course building a simple dynamic website with Node.js, which was good.

I do not understand how do deploy it i mean make it a live site, I know how to do this for static sites and sites that use php and support php via the hosting.

I was hoping to lean how to use node to process a form since I am making a online version of my CV which has a contact me form. Its a shame to know some Javascript and not know how to use it for something as simple as form processing..

Steven Parker
Steven Parker
231,269 Points

I haven't taken it myself yet, but it appears that Basic Form Processing is one of the topics covered in the Express Basics course.