Node JS example with HTTP module

Slightly more practical example of an exercise carried out with Node.JS and the HTTP module, which is used to implement HTTP communications with NodeJS.

This is the last block of the talk broadcast on .com by Alejandro Morales. In this case we are entering a more practical field, which will allow us to see a first example in Node.JS. It is also the fifth article of the , in which we already get down to work to start working with source code in a first little program.

This is the module that helps us to work with the HTTP protocol, which is the one used on the Internet to transfer data on the Web. It will help us to create an HTTP server that accepts requests from a web client and will serve as a climax to the introduction to Node.

Since we want to make use of this module, in our example we will start by requiring it with the “require()” statement.

var http = require(“http”);

Note: There are other modules that create servers such as “net”, “tcp” and “tls”.

From this moment we have an http variable that is actually an object, on which we can call methods that were in the required module. For example, one of the tasks implemented in the HTTP module is to create a server, which is done with the “createServer()” module. This method will receive a callback that will be executed every time the server receives a request.

var server = http.createServer(function (request, response){ response.end(“Hello WebDevelopment.com”); });

The callback function that we send to createServer() receives two parameters that are the request and the response. We do not use the request for now, but it contains data from the request made. We will use the response to send data to the client that made the request. So “response.end()” is used to finish the request and send the data to the client.

See also  Define a pattern to make a background with Photoshop

Now I’m going to tell the server to start up. Because so far we have only created the server and written the code to be executed when a request is made, but we have not started it.

server.listen(3000, function(){ console.log(“your server is ready at ” + this.address().port); });

With this we tell the server to listen on port 3000, although we could have put any other port we liked. In addition “listen()” also receives a callback function that would not really be necessary, but it helps us to do things when the server has started and is ready. Simply, in that callback function I indicate that I am ready and listening on the configured port.

Complete HTTP server code in node.JS

As you can see, in just a few lines of code we have generated a web server that is listening on a given port. The complete code is the following:

var http = require(“http”); var server = http.createServer(function (request, response){ response.end(“Hello WebDevelopment.com”); }); server.listen(3000, function(){ console.log(“your server is ready at ” + this.address().port); });

Now we can save that file anywhere on our hard drive, with a .js extension, for example server.js.

Note: If we are in Windows we could save the file in a folder called node in c:. The path of our file would be c:/node/server.js.

Launch the file with Node.JS to start the server

Now we can execute with Node the file that we have created. We go from the command line to the folder where we have saved the server.js file and execute the “node” command followed by the name of the file that we intend to execute:

See also  Using Ajax very easy with jQuery

node server.js

Then, in the command console, the message informing us that our server is listening on port 3000 should appear.

The way to check if the server is really listening to client requests on that port is to access it with a browser. We leave that command line window active and open the browser. We access:

http://localhost:3000

Then the message “Hello .com” should appear.

Conclusion on the introduction to NodeJS

With this example up and running we have completed the material disclosed in the #nodeIO introductory NodeJS webcast. The truth is that, put into words, the amount of information that Alejandro Morales @_alejandromg gave us is impressive, to whom I send my most sincere thanks and congratulations for this magnificent exhibition.

In subsequent webcasts we will continue talking about NodeJS and making other examples from scratch that can be released to us in the “Node” world.

I end with a couple of recommendations from Alejandro himself:

  • Learn Javascript to be able to move to NodeJS later with guarantees. It is more important to master Javascript itself than to have an idea of ​​other server-side programming languages.
  • If you have a sufficient base of Javascript, spend at least a week, about 40 hours, to learn NodeJS and make some interesting examples. show it to your friends and promote what you have learned and you will be helping the NodeJS community, spreading this technology.

conclusion

We will continue from here commenting on some other actors in NodeJS programming, such as events and streams, and making slightly more complex examples. If you want to learn the things that we will see in the next articles, I recommend you watch the second webcast

Loading Facebook Comments ...
Loading Disqus Comments ...