After you visit the web page, press Ctrl-C in your terminal to end the program.
Let’s take a closer look at what’s going on in this code. I don’t expect you to understand it right now. I’ve added comments to the original code. Just try to get the gist of it!
var http = require('http'); var server = http.createServer(function(req, res) { //send response to client res.writeHead('I created this server!'); // finish the response res.end(); }); //the web server is listening on port 8080 server.listen(8080);
Node is doing most of the work for you with built-in code. The very first line is calling the http module, which, behind the scenes, has the code to turn your running program into a web server. The require keyword is giving the new variable http access to a whole lot of saved Node code and functions that can create a web server.
In the second line, the code is calling a function called createServer. Whenever anyone connects to your web server, the code in that function will be executed.
The last line tells the server you created to start listening for incoming requests on a particular port (in this case, 8080).
Lynn Beighley is the author of dozens of tech books and even more articles. She's had lots of her short stories published in lots of places. She's an editor for Manning Publications and she's currently writing a YA novel that she hopes will encourage girls to try a bit of computer programming. She got an MFA a few years back. She's on twitter as @lynnbeighley and would enjoy hearing from you.
Short but to the point. Unfortunately, I don't think this book had enough practical samples to make it a worthwhile guide to actually learning javascript. It's a good primer if someone is curious what javascript is, and how it can be used, but I'd not recommend it to anyone for actually learning the language. Add to that, the primary example is a twitter bot that will no longer be valid once twitter changes their interfaces in August of this year, and this book looses it's utility almost entirely after that point. Still, if you're trying to find out what all the hype is about, this book will serve as a good jumping off point for further study.