Jumpstarting Javascript

Jumpstarting Javascript
Authors
Beighley, Lynn
Publisher
Maker Media, Inc
Tags
utilities , computer science , open source , web , browsers , programming , computers
Date
2017-01-01T00:00:00+00:00
Size
3.63 MB
Lang
en
Downloaded: 305 times

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).