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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Michael Cook](https://uploads.teamtreehouse.com/production/profile-photos/10325482/micro_10441420_10152452040502048_3507177777575354946_n.jpg)
Michael Cook
Full Stack JavaScript Techdegree Graduate 28,975 PointsI don't understand the code in the www.js file
There is a lot of boilerplate code already written for this course that doesn't make sense to me (and its frustrating that there's no explanation of it).
In the www.js
file the http
module is required and a server is set up.
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
Why is any of this necessary? With Express you can just use app.listen(port)
. I don't see why all the above code is necessary.
There is some other stuff that seems unnecessary as well. Like this function:
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
Why would you need to "normalize" the port number? For what purpose? It's a bit frustrating how this stuff is just written for you with no explanation.
1 Answer
![Guil Hernandez](https://secure.gravatar.com/avatar/bad4b01a177d5803f443e11fa7d55c59?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Guil Hernandez
Treehouse TeacherHi there. As mentioned at the beginning of the workshop, I used the Express application generator to spin up the Express app. :)
Michael Cook
Full Stack JavaScript Techdegree Graduate 28,975 PointsMichael Cook
Full Stack JavaScript Techdegree Graduate 28,975 PointsRight, thanks. I do like your course, and actually all your courses. Just getting to the end of the FullStack JS track and finding myself getting frustrated and a bit overloaded. Probably just need to sleep on a lot of this. Thanks again Guil.