Node.js – Hello World Podcast Episode 2

A 10-minute introduction to Node.js and its asynchronous event-driven development model. Using the simple “Hello, world.” example provided in the project documentation, Jason will explore more of the API, objects, and the “event emitter” design pattern for a more thorough look into this fascinating platform.

Hi, Jason with unmatchedstyle here. Today, we are going to talk about Node.js. Node.js is evented I/O for V8 JavaScript. We are going to get into this real quick.

nodejs

Let’s go ahead and start with their example. First, we are going to pull in the HTTP library that’s built in to Node.js. This is part of their standard library that ships with the package. After you have installed Node.js, you have access to this. Let’s go ahead and pull Node.js in here or pull the HTTP library portion of Node.js. The require statement can pull in the standard library modules or it can pull in local files. So, in our case, we are going to pull in HTTP which is going to look something like this. But if you want to pull in a local file, something in the same directory as this script my local file. You don’t need the .js at the end of it. Just the name of the module itself would be fine.

node01

So, now we have to do something with this. Literally, you can run just this but let us go ahead and create this server like they have in their hello world example. Now, we are not going to use the factory method that they provide. This is the static method that would return a server object, an HTTP server object.

And in this method here or this function, this call back that you are passing is automatically appended to the list of functions that listen for the request event. Again, this is all evented I/O. So, if we look at their documentation, what that means is when you create a new server, here on the left you see the various events that you can listen for.

The one that we are talking about is the event request. Now, I’m not going to use their shortcut. I want to be as verbal as possible so we can really get a good feel for what is going on. Let’s go ahead and substantiate the server object so we are just going to create a new server, a new HTTP server. So, now we have our new server.

node2

It doesn’t really do anything but we can go ahead and have it listen. Let’s take a look at how the syntaxes for listening, all listeners have a port and a host name. It is what we really need. We don’t necessarily have to pass the call back. So, we have to listen on port 8080 and the loop back interface. Now, this will run. It won’t do anything because we are not listening for the request.

We are not listening for any events whatsoever. We can go ahead and give you a feel for what is going on here. It would be good if we actually have to listen properly. So, now our server is listening on port 8080. If we go ahead and look at it, nothing really is going to happen. Actually they are going to hang up because we are not responding.

It is an HTTP server. It is trying to get the request so obviously it is not going to work the school head and kill it. Let’s head back to documentation and let’s listen for that request event. Now, to add an event listener, all you do is use the on method. So, server.on request, you almost read this as a sentence, right. And now, the on method.

It’s a call back in the formats specified by that event initially. This is an event in meter model or event in meter pattern that they are using. So, the two arguments starting the past automatically to our function, to our call back. You can see here in the documentation our request and response.

Now, each one of these objects that are passed in to our function are also documented. So, the first thing that we are going to get is a server request. The request that client has made to us. This is all cleaned up through the Node.js HTTP server object. We are getting a nice clean request. There is nothing dirty in here that we would have to clean up.

And we can get things like the URL, the method post get, put delete, all that sort of thing. We also get the headers that are coming in but we are actually going to do is we are going to short circuit this and get it looking like their example. We are going to go ahead and use the response. Let’s go ahead and look at their example up here.

node3

We are going to write a header back to the client, back to our browser. So, let’s write that header, response. write here. We want to simulate 200 response code which means everything is coarser. That’s in it some headers to explain. Now, I have an additional method here that isn’t in the example right. Right you can use this as many times as you want. You can call this inside of the loop or what have you. You can only call response.end once. We are not going to use response.write at the moment.

We are just going to set Hello World to our browser. Now, if we run this again, we are going to see here in our browser is Hello World. This is a text claim context type. So, we are seeing it in the preformatted text there. We can change this to text, HTML. Put a little H1 in our response to format properly. Of course, we have to stop server. Restart it.

node4

There we are Hello World in HTML. OK, we are going to keep the HTML here and I am actually going to break sort of the whole design idea behind Node.js. We are going to synchronize file to pull in an external file. The reason is I want to show you a little bit more about the request and the response mechanism and so taking time to write in an evented streaming mechanism to pull in a static file.

We are just going to go ahead and use this synchronous capabilities here. Now, this is going to require us to pull in another module. We must pull in the file system module. Now, we have access to all these file systems functions. A lot of these are just static functions. You can use them however you want to use them. For instance, we can do a file system, read file sync.

node6

This is a synchronize version of the asynchronies read file which you actually see up here. File system read file, this is the asynchronies style which you should prefer when you are using Node.js. But we are going to use read file sync. So, which we can do since this is a static function built right in this module. We can simply read files sync and we have a local file in the same directory called static.html. I pull it in a new here.

node5

This is our static HTML file. You can see it has a very simple form that we are going to use to test some more of the request and response functionality. So, let us pull that in. Now, if we restart our server, we have a nice form that we can submit a value submitted and no post back to our server. Now, what we have wanted to do is say this is a login form or whatever sort of form, contact form that you want to capture the response.

node7

You don’t want to capture it when it stop posting. So, if we check out the request object in documentation, go back to HTTP server request, you’ll see that we can test the request method which would be to get post whatever. So, if we simply add a conditional, you can test that to see if this has been posted back.

node8

Now, all we are going to do, we can actually be a little hacky here and add the hitter above and using the response rate as our console effectively. We can test and print out whether or not our form has been posted back. Now, if we restart our server, you don’t see this message. You don’t see this response.write not called because our request wasn’t post type.

Now, if we simply submit our form, our form has been posted and this is where you could insert this into the database. You could handle this form elements, you can process that data. Now, this is just a very trivial introduction using basically the Hello World example to give us something to look at, something to play with. You don’t want to be doing the read file sync operations unless you absolutely have to.

But in your all these other modules that come standard with Node.js. Of course, we explore the file system one, the file system module. You can do all sorts of things with that. You can also do a DNS, your own manipulation, path manipulation, acquired stream passing and generation. And you can also write your own modules and do a whole bunch of really cool stuff and actually there are a lot of cool libraries out there for Node.js that you can certainly want to check out.

So, I hope you enjoy this. This has been our brief introduction for Node.js. Thanks for watching.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

More News & Articles

The Essential Guide to Getting Started on Freelance Writing

The Essential Guide to Getting Started on Freelance Writing

Explore the lucrative and fulfilling world of freelance writing with our essential guide. Learn about specialties like blogging, social media, article, and technical writing. Build a portfolio, find work, set up your business, and discover the potential earnings. Embrace the freedom of working from home and follow tips for success in your dream career.

Securing Your Website: A DevOps Security Checklist

Securing Your Website: A DevOps Security Checklist

Learn how to secure your website with a comprehensive DevOps checklist. Dive into SSL/TLS encryption, password practices, and more. Discover the power of Content Security Policy and safeguard your online presence effectively.

EMAIL NEWSLETTER