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 trialRich Zimmerman
24,063 PointsWHY use http.createServer() Node.Js
So when we first learned to pull and parse data from a JSON object to the console, we didn't create a web server. But when doing the next example where you do the same thing, just display results into an html page, we create a web server with http.createServer.. I'm just curious as to why, what exact purpose it serves and why we didn't have to create one in the first example.
Thanks, Rich
1 Answer
Chris Shaw
26,676 PointsHi Richard,
NodeJS out of the box is simply a console application which means we can't serve HTML files for example as the output would appear as plain text, the reason we need to use http.createServer()
is because it creates an HTTP server that binds to the given port that your browser can connect to and retrieve static HTML pages from along with JSON requests for example from a micro API.
Hope that helps.
Rich Zimmerman
24,063 PointsRich Zimmerman
24,063 PointsThat helps a lot. Thank you.