What you'll learn
- Install the LTS release of Node.js and NPM and confirm installed versions with node -v and npm -v
- Use the Node Version Manager (NVM) to switch between Node.js and NPM versions
- Run JavaScript snippets in the terminal to test syntax outside the browser
- Create a core Node.js web server with the built-in http module that listens for requests and sends responses
- Explain how multiple Node.js servers communicate with each other and with external clients to complete a request
- Build and run a date and time server in a GitHub Codespace opened at a specific repository commit
Free 55-minute video lesson with a full transcript and knowledge checks. No account needed to watch.
Setting Up Node.js
Installing and setting up Node.js and NPM is dead simple - or is it? In this video, we start our service development journey by installing the latest versions of Node.js and NPM and see how to check our local versions.
Knowledge Check
In this context of Node versions, LTS stands for ________.
Using the Node Version Manager (NVM)
We now have the latest Node and NPM versions installed. However, your job as a Node services developer will require you to be able to switch between versions quickly and easily depending on the service you're working on. In this video, we see how to do this by installing and using NVM.
Knowledge Check
The `nvm use` command will fail unless we've already installed the version we're trying to switch to using `nvm install`
Working with Node.js in the Terminal
Next, let's take a look at how to run simple Node.js commands in the terminal, both with the Node.js REPL and the node -e command. This both gives us a great way to test our code, and also allows us to easily practice what we learn.
Knowledge Check
What will the following command output to the console? (try it for yourself): node -e "console.log("Help")"
Creating a Simple Web Server
And now, it's time for the piece de resistance - creating a simple web server using only the Node.js Core (i.e. without any additional libraries such as Express). In this video, we see how to create and run a server, as well as how to test it with the Node.js REPL.
Knowledge Check
Once we've created a Node.js core server, we can start it up by calling the ___________ method.
Challenge & Solution: Date and Time Server
Now it's time for a challenge! In this challenge, you'll be asked to use the concepts you learned in this skill to create a server that tells the client the current date and time. Watch the video for more information.
Oh, and here's the link for the Github commit. This is where you'll want to create a Codespace from in order to complete the challenge.
Finally, now that you've attempted the challenge, I'll show you the solution that I came up with and walk you through the implementation process.
As I said in the solution video, here are some other simple projects you can implement in order to practice even more:
- A server that sends back random jokes or inspirational quotes
- A server that keeps track of how many requests it has received and sends that back to the client - this could potentially also be used as a "take a number" server, kind of like those paper number rolls they have at the meat counter at the supermarket.
- A "Magic 8-Ball" server that responds randomly with variations of "yes", "no", or "maybe"
Knowledge Check
How difficult was the challenge for you?
This interactive assessment is available in the full learning experience.
View Transcript
Setting Up Node.js
0:00Hi Sean here and welcome to this skill where we're going to get started with service development in Node.js and in this first video here we're going to do something very important and that is we're going to get started by setting up Node.js as well as NPM on our local computer and we 're also going to see how we can better manage versions using something called NVM. Now we'll go over the terminology etc in more detail a little bit later but to get started here just know that we need to download and install Node.js so that we can run JavaScript outside the browser right this is kind of the JavaScript runtime that allows us to do that and NPM comes bundled by it by default and NPM is a pretty interesting and very very useful tool that allows us to install other people's code in the form of something called packages into our projects so if you're not familiar with those things yet never fear we'll talk about them in much more detail a little bit later but let 's just get started here by installing Node.js and NPM so what you're going to want to do is head over to Node.js.org
1:16and download the LTS that is long-term support version of Node.js now one thing to just keep in mind as we get started is that the version that you're seeing here is probably different from the version that you're going to see when you go to this website because well as versions tend to do they advance right so you'll probably see 20 point something or 21 point something or you might even see 30 point something who knows but anyway we're going to download the long-term support version and you can do that just by clicking this button it should automatically download it for the appropriate operating system and once you've done that once it's downloaded completely you can click on that and that should get the installer started so let's just click on continue we're going to click on continue again and agree and then we'll click install now just before I do this before I put in my password in order to start the installation process I just want to point out that this process is pretty much the same way that you would go about updating Node.js as well all right so let's just take a short detour here and what you're going to want to do in order to follow along here is just open up a terminal you can do that in
2:34either just on your computer by opening whatever your operating systems equivalent of the terminal application is on Mac or what I would recommend is to get an IDE like visual studio code and just use the built-in terminal by going to terminal and then new terminal and that should open up something that looks like this here all right so what we're going to do here is we're going to check the current version of Node.js as well as NPM to see if you already have it installed right I'm just going to show you here that I already have this installed to confirm that this is in fact going to update Node.js and NPM for me so if I run Node.V right now what that's going to do and this is Node.version by the way this prints out the current version of Node.I have installed and then NPM-V and this is the current version of NPM that I have installed so going back to the installation process what you're going to see is that that will update the versions of both Node and NPM and if you ran those commands and just saw some sort of error saying that the command node or NPM wasn't available meaning that you don't have NPM or Node installed already well this is going to install that in the first place so let me just put in my password here and if we click install software that
3:52should run for a short bit and install Node.js and NPM for me that's going to be the new version so let's click on close now and you should see that it says Node.js version 20.10.0 again you might see something different than that exact version but in my case that's the one I'm installing and then we see the NPM the NPM version which is 10.2.3 you'll see something different there as well so let's close this now and well in my case it's going to ask me if I want to move the installer to the trash which I will and let's go back now to our terminal and what you should be able to do now is rerun Node-V and see that it's now that updated version as well as NPM-V and that will be an updated version as well cool so that's how to install Node and NPM in the first place so I hope this has been informative for you and I'd like to thank you for viewing.
Using the Node Version Manager (NVM)
0:00Cool, so that's how to install node and NPM in the first place. So the next thing that we're going to take a look at is we're going to see how we can use something called the node version manager and VM, not to be confused with N PM, which is the node package manager. We're going to see how we can use this NVM thing to manage the versions of node and NPM on our computer. Now the first question you'll probably have is why do we need to bother changing the versions of node and NPM on our computer back and forth, right?
0:35In other words, if you currently have the most up to date LTS version of node, which right now again is 20.10 point zero. Well why on earth would you ever want to change that to something like 16.1 point zero, right? Or you might even want to go lower than that and go back to node 10, let's say. Well, the reason that you might want to do this, and this is very, very important as a services developer, the reason that you might want to do that is because certain pieces of code might have been written for a specific version of node, right?
1:11In other words, if you're working on an application that's, let's say maybe five years old, then chances are that application isn't going to be designed for the latest version of node, right? But you would probably want to do in that case just to create a solid local testing environment is you'd want to go back to whatever that version was and NVM can help you do that. So let's see how to get started here with NVM. Now NVM by the way is an open source project.
1:43You can find it on GitHub under NVM SH slash NVM. And if you scroll down here, you should be able to find the instructions for installing this locally. In my case, that's going to be right here, right? You can see that it's under the heading, installing and updating. And in my case, I'm just going to use this C URL command. And that's basically going to download the install script, which will run and install NVM on my computer. So I'm just going to copy this. We'll just click copy.
2:14And then I'm going to go back to my terminal and paste it. And if you hit enter, it should run for a little bit. In my case, you may see that it says NVM is already installed. Now again, you probably didn't have NVM installed already. So you might see something, you might see output that looks a little bit different from what we see here. But at this point, you should have NVM installed. So the next thing that you're going to want to test is whether or not that installed successfully.
2:45So the way that you can do that is very similar to what we did with node and N PM. And that is by running NVM dash V. All right. Now, in my case, what you're going to see is it says NVM dash V. And in your case, you'll probably see this as well. Now that's just because in general, when you run some kind of install script like we just did, you actually need to either restart your terminal or manually source, whatever you just installed in order for those changes to take effect and be able to use that new command.
3:17All right. So we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that.
3:47And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that.
4:17And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that.
4:47And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that.
5:17And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that.
5:47And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that.
6:17And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that.
6:47And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that.
7:17And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that.
7:47And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that.
8:18And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that.
8:49And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that.
9:19And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that.
9:50And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that. And we're going to do that.
10:20And we're going to do that. And we're going to do that. And we're going to do that.
Working with Node.js in the Terminal
0:00All right, so now that we've seen the basics of installing node JS and NPM and we've also seen how to use the node version manager to switch between versions of node and NPM pretty rapidly. The next thing that I want to show you is how we can run snippets of node JS code, right? That is JavaScript code. Well, two ways really that we can run JavaScript code in the terminal. Now the first way here and this way is very, very useful when you want to test out snippets of code, you want to make sure that you understand the syntax of JavaScript, right? Sometimes it happens to me where, you know, I get mixed up with the syntax and I run into some kind of syntax error.
0:41So I want to actually run that piece of JavaScript code in the terminal to try and play around with it and see if I can get it right. Well, you can do that all by opening up the node JS terminal and you can do that just by running node with nothing after it in your terminal. And by the way, in case you haven't caught on to this yet, I'm going to be using visual studio code for all of these demonstrations. If you want to download that, your best bet's just going to be to Google download visual studio code, and that should take you right there.
1:14So anyway, once you've done that, again, you can open up a new terminal here by going to terminal new terminal. That should get you to where I am right now. So let's run the node command now. And what you're going to see is that's going to say welcome to node JS V 20 point 10 point zero, right? That's the currently active version of node JS that we installed using NVM. And instead of the normal terminal, what you can see here is this is basically just a little terminal that we get to run node JS snippets in right so basically again JavaScript code.
1:51So first things first, let's just try console.log Hello. And what we're going to see there is sure enough, it'll print out hello to the console and underneath that you're going to see this undefined thing. Basically, all that that means is this is the return value of whatever statement, whatever code we just ran in the terminal. And since console.log doesn't return anything, that's why we're seeing undefined. All right. Cool. So anyway, now that we've, now that we've opened up the node JS console, let's try something a little bit more interesting.
2:26And I really want to tie this back in with the whole node version manager thing and working with different node versions. Let's try something here that's going to be a little bit different depending on the version of node JS that we currently have active. So what I'm going to do is I'm going to say const util equals require util. All right. And then I'm going to say const read file promise equals util.promise if I, and then I'm going to call that on require FS dot read file and the read file is on require FS.
3:08And then basically what this is going to do is it's going to convert this normal read file operation, right? This read file function on the FS package into a promise, right? It's going to convert it from a callback based function to a promise based function. And the thing about this is if we run it here, it should work just fine, right? We can now say read file promise, and then we'll pass some kind of code into it and you might want to actually create a file that we can read here.
3:40And by the way, what I did here is I just created a new directory called JS NSD example code. If you just create a new file here and call it something like my file dot TXT. And then we'll just add something to this file. We'll say hello from a file. All right, well, we should be able to read the contents of this file now by saying read file promise my file dot TXT. And we'll add the UTF-8 encoding there.
4:11And by the way, if you're not familiar with these functions, I'm not going to be going over the specific details of doing things like reading files and Node. js, etc. That's something that you can find by taking a look at one of my other Node.js basics courses. So feel free to check that out. But anyway, what this is going to allow us to do now is say dot then. And this will give us access to the contents of the file, which we can just log out to the console. We'll say something like contents, and then we'll say contents or console dot log rather.
4:42Contents, okay. So if we close that function off and hit enter, sure enough what we'll see is after this promise thing, which is the return value of read file promise. Etc. We see that it prints out hello from a file to the console. Now where I'm going with all of this is this code worked because we have the current version of Node.js that we have. However, if we were running a different version of Node.js, things wouldn't work out quite as well.
5:13All right, so what I'm going to do is exit out of the node terminal, which you can do with control C, pressing that twice. That is, and I'm going to change the version of Node.js that we're using to something quite a while back. So actually, what we're going to do is we're going to say NVM LS so that we can see the old old versions of NPM that we can install or of Node rather. So let's install Argon. This one is by all appearances, the oldest one we're able to install. So let's try saying NVM install. We can just use LTS and ups I spelled install wrong LTS slash Argon.
5:49And that should install version 4.9.1, very old version, and basically set that as the current version of Node, which we can verify by saying Node-V. And sure enough, we see 4.9.1. So let's open the terminal back up and we're going to try doing the same thing that we just did, right? Promissifying and reading a file, and it's just not going to work. You'll see how exactly in just a minute, but let's just open up that terminal by saying Node.
6:20And if we try running our code again, and by the way, you can do that just by using the up arrow, let's scroll back up to this const read file promise equals util.promissify thing. Now, one thing that's interesting to note is that you don't actually need to import util here because it's just included in the scope by default. But if we try and run this file, what we'll see is that it says util.promissify is not a function. Now, the reason that you're seeing this error here when the code worked before is because in this older version of Node, there was no function called promise ify on util, right?
6:58It hadn't been added yet. Whereas as we already saw in a more recent version, a very recent version, in fact of Node.js, that function worked just fine. So this is just one example of many different pieces of code that will either work slightly differently or not at all in different versions of Node.js, which again just kind of underlines the importance of using the correct version and node and being able to switch between those versions with NVM.
7:28So anyway, now that we've seen how to run code in the Node terminal and just some implications of running code under different Node versions, let's switch back to the later version. And we can do that by saying NVM use and we can use its alias here. We'll say LTS slash iron. And that will give us 20.10.0. And by the way, in case I didn't mention this, I don't think I did. Another advantage of using these nerdy aliases here is that it will actually give you the latest update on a given version branch.
8:02Right, so if you want to picture the way that Node versions work like this, basically the way that they work is for any given Node version, let's say four, five, six, all the way through. Here we have 18, 19, 20. Well, obviously each version sort of builds off of and improves the one before it, right? So 20 builds off of 19, 19 builds off of 18, all the way down the chain, right? However, because of the fact that given a specific version of Node JS, there are many, many applications that are dependent on that version and require that exact runtime in order to run correctly.
8:44Each of these versions is kept around and improved just sort of as an independent branch. Right, so even after Node JS version 20 is released, there 's still going to be changes and improvements on 18, 19, all the way through. So by using those aliases, right, by referring to LTS slash iron or LTS slash Argon or whatever, that's going to bring you to the latest release on that branch.
9:16So in other words, you're not going to need to keep your eye on the Node JS news for when 20.10.1 comes along. You can just refer to it by its alias and that will make sure that you're using the latest version in that branch. So anyway, now that we've switched back to a modern version, a much more modern version of Node JS, that is, let's run another piece of code here. And this is going to be basically just a command that we type into our terminal . So what this command is going to be, it's going to be at node dash E.
9:48And then in double quotes, we're going to say something like console.log. Hello . All right. Not a not the most exciting piece of code around, but there is a point here as you'll see. And if we hit enter, what we're going to see is that sure enough, hello gets printed out to the console. Now this node dash, anything, what this does. And the dash E stands for execute, by the way, is it will execute whatever code we have in this string. So this is basically a way of running individual small pieces of Node JS code, right?
10:21So this is a server side JavaScript code without actually having to open up a node terminal and type commands into there, right, without having to open up the read evaluate print loop, our EPL, as that's often referred to. All right. Now the reason that I'm showing you these things, right, how to use node dash E and how to use the node terminal in order to run just basic snippets of JavaScript code is because these are two very useful tools for testing the web servers that you create with Node JS, right? So what we're going to see very shortly is how to create a basic server using Node JS as well as several Node JS libraries, such as Express and Fastify.
11:06Well, if we have a server running here that we want to test, we need some way of sending requests to that server so that we can see how that server behaves and what data it responds with. Well, there are obviously lots of options for what we can have over here on the client side sending those requests. Well, in many cases, the easiest option is to have that thing on the client side be another piece of JavaScript code, right? So using what we just saw, if we have a server running over here again, and we want to send a request to it, we can just write a little piece of JavaScript code that will actually send a request to that server, and we'll be able to see there right in the console, what that response was, and we'll be able to do things like manipulate the response, look at it in more detail, etc.
11:56In that case, we just have this little console thing that's making that request and basically processing the response that we're getting. Anyway, to make a long story short, the reason I showed you those two ways of executing server side JavaScript code is that frankly they just make great testing tools. So anyway, I hope this has been informative for you, and I'd like to thank you for viewing.
Creating a Simple Web Server
0:00All right, so at this point, we've got pretty much everything set up and we've covered pretty much all of the topics that are going to lead us up to the main feature of this section. And that is creating a simple web server using Node.js. All right, now a server is really going to be the main unit that we end up building our services out of, right? So really all that a server is is a program that sits there and listens for requests, right?
0:30These are incoming messages, if you will, from somewhere on the Internet, right ? Somewhere across the Internet. And when the server receives a request, it's going to execute some sort of some piece of code and it's going to send some sort of response back to whoever sent that request. Now the cool thing, and this is something that we'll get into later, is you can actually have an application that consists of multiple servers, right?
1:01So you could have, let's say, three or four or a hundred servers, and these servers can actually talk to each other as well as to external clients, right? So in other words, this server here might get a request from some client, and in order to complete that task, right? In order to do whatever it is that this client wants it to do, it might have to communicate with these other servers. So it might have to send its own request to this server here, that server might respond, then it might have to send a request to this one here.
1:36This one then might have to send a request to this one, and so on and so forth. So as you can see, you can create some very interesting structures here, all right? And there are benefits to structuring your applications like this versus just having a single server that contains all of that functionality. And that's something, again, that we'll talk about later, but I just wanted to show you kind of what a server is and how it fits into this whole idea of service development in Node.js.
2:07So, and by the way, this server here at some point, if it all goes well, would send back a response to the original client. Cool. So anyway, with all that said, it's obviously going to be very important that we know how to set up and run a basic Node.js server. So let's take a look at how to do that. Now, obviously it's possible to set up a server just in your terminal. You could do that if you wanted to, right? You could just run the node command and then set up and run a server from here.
2:40However, that's really not the best way to go about things because, well, you 're probably going to want to have the code for that server in a reusable format , which is why not surprisingly, we generally create our servers inside a JavaScript file. So let's create a new file here. And again, I'm just, I created this JS NSD example code directory. And you might want to do that as well. Just create a directory to hold all of your code. And let's create a new file inside of here. And we'll call this something like core node server.js.
3:17But the reason that I'm specifying core node server is because, as we'll see in later skills, there are plenty of frameworks in Node.js that greatly simplify the task of creating and running a server. But, you know, just to start at the very beginning here, we're going to see how to create a server without any other frameworks, without any other libraries. And, well, this is basically what all of those frameworks and libraries are built off of in the first place. So it's probably a good idea to at least see how this works.
3:50So really the main thing, the main package that you're going to be using when you're creating a core node server, right, with only the node.js core packages is HTTP. So what we're going to do is we're going to say const HTTP equals require HTTP. All right. And then what we're going to do is we're going to create a server by saying const server equals HTTP dot create server. Now, not surprisingly, this create server function is going to create a server that will then be able to start up and that will basically just sit there and listen for requests.
4:30So the way that we start up a server is by saying server dot listen, and we get to specify something called the port. Basically, this port is basically just a number that allows us to distinguish between different connections or different services as we'll see later on that are running on the same computer. So as you'll see, when we actually create applications that are made up of multiple servers, we 're going to need to have this port number, which for now we'll just use something like 3000, which is a pretty commonly used port number at least for development.
5:06We're going to have to come up with different numbers for each of those different servers, right, so we might need to have one server running on port 3000, one server running on port 8000 and one server running on port 8080. Let 's say. All right, so anyway, once we've started our server, we're probably going to want some sort of output in the console in order to let us know that the server has started up successfully. And the way that we do that is by passing a callback function as the second argument to server dot listen.
5:38And that's going to look like this. We're just going to use a arrow function, and we'll say console dot log, and we'll print out something like server is listening on port 3000. Okay, and that is pretty much all we need to do except at this point our server isn't actually going to do anything right we've told our server to start listening but obviously, you know, when a server receives a request as we said, right, when we have our server and a request comes in, we're going to want to have some sort of code that gets executed and sends back a response to whoever the client was that's a really poorly drawn computer there but anyway you get the idea.
6:19So the way that we do that, and this is going to be a very popular way of doing things in case you're not familiar with working with no JS, we're going to define another callback function and pass that to HTTP dot create server so here's what this is going to look like we're just going to have a simple callback function. And inside here will print something out to the console by saying something like console dot log. I received a request. Okay, and that's all we're going to do right now. Now this is not really a complete implementation for this callback function because as you'll see in just a minute, it doesn't send back any kind of any kind of response to the client.
7:03We'll see how to do that in a minute as well. But nevertheless, it will do something when it receives a request. So what we're going to do here is we're going to run our server that we just created by saying node core node server dot JS, right, this is going to run our server file. And if we hit enter, what you'll see is that it prints out server is listening on port 3000, and then it just sort of sits there, right? Well, the reason that it's doing that is because generally the server programs that we create will run continuously, right, so unlike regular node JS scripts or scripts of any other programming language for that matter.
7:44When we create servers, that server is just going to run continuously, usually until we manually stop it or it crashes, let's say. So right now, again, our server is just sitting there, waiting for some sort of request to come in. So let's test this thing out now by sending a request to it. Now, the probably the easiest way to test this out is a way that we saw in a previous video here, and that is to just open up a new terminal and go to the node, reevaluate print loop.
8:20All right. And then we're just going to use the same HTTP package here, right, the same HTTP module to make a request to our server that's currently running. So here's what this is going to look like. We're going to say const HTTP equals require HTTP. All right. And now that we've imported the HTTP package, what we're going to do is say HTTP dot request. And the URL that we're going to send this request to is the first argument that we passed to this function.
8:53And that's just going to be in our case here, HTTP colon slash slash local host , because this server is running locally on our computer. That's what local host refers to generally. And then we have this this colon 3000 thing that refers to the port number. So again, if we had multiple servers running on our running locally on our computer, we could change this port number to 4000 5000. 8000, 8000, whatever, in order to refer to different services, right, in order to send our requests to different servers on our computer.
9:32So we're just going to send it to local host 3000 right now. And basically, that's all that we need to do, right. And in order to actually send this request, what we need to do is add dot end on it with parentheses after it. So basically what this is doing, it's creating this request that it's going to send to this address, right, this URL. And then by saying dot end, that's actually going to send it. So let's hit enter here. And if we go back to our other terminal now where our server is running, sure enough, we'll see that it says I received a request.
10:07Now again, this server that we just created isn't really fully functional yet because it's not sending back any kind of response to the client. And that's the next step here. You see, in order to send back a response to the client, we have to actually use an argument that's going to be passed to this callback function automatically. And there's actually two of them. One is called request and one is called response. They're usually abbreviated R E Q and R E S. And the R E S one, the response is what we're interested in here for the time being.
10:42We'll talk about the request argument a little bit later. But anyway, what this is going to look like is we're going to print out, I received a request, but then after that in order to send a response back to the client, right, in most cases, the client will actually think that something went wrong or think that there was some sort of error if we didn't send back a response. What we're going to say is we're just going to say res dot end and we get to basically pass whatever we want here. And that will be sent back to the client back to whoever sent the request as a response.
11:16So let's say response dot end. And in here, we'll say something like hello from the node JS server. And if we restart our server now, which we do have to do in order for that change to take effect, all right, so you can stop it by saying, control C. And then we just run our server again by saying node core node server.js. And if we go back to our other terminal now, what we can do is we're actually going to run this same kind of function again, HTTP dot request dot end.
11:49But in order to actually allow ourselves to see the response, we're going to add a callback function to this HTTP dot request function. This is going to be the second argument here. And basically what this is going to look like is we're going to say response. All right, so this is the argument that gets automatically passed to this callback function. Again, callback functions are a big thing in node JS. They basically just give us a nice way to handle code in an asynchronous manner .
12:22And what we're going to do is we're going to say response dot pipe. And then we're going to say process dot standard out. Now, basically what this is going to do, don't worry too much about this res dot pipe thing. We'll talk about that in more detail later as well as process dot standard out. But this is just going to take whatever response we get back and print it to the console. So let's just hit enter here. And sure enough, what we see is that it says hello from the node JS server, right? It just prints that right to the console there.
12:53And if we go back to our other terminal here, we'll see that our server sure enough printed out, I received a request. So that's the basic idea of creating a simple web server using the node JS core . And really all that our server is doing right now is it's just handling all requests that come in by sending back hello from the node JS server. And this might seem a little bit simplistic, but really the functionality that you see here is the foundation of building services in node JS.
13:26So anyway, I hope this has been informative for you and I'd like to thank you for viewing. [BLANK_AUDIO]
Challenge & Solution: Date and Time Server
0:00All right, so now that we've learned the basics of working with Node.js and we 've seen how to create a simple HTTP server It's time for us to solidify what we've learned by doing a challenge now The challenges in this course are going to be a little bit different than some of the challenges I've done in other courses Because I'm gonna be trying something new which is something called code spaces now code spaces in case you've never heard of it It's still at the time of recording a fairly new Fairly new thing code spaces are basically an integrated version of visual studio code that you can use directly on GitHub it's they're they're really cool. I think you're really gonna like it and the The reason I'm using these is because it will allow you to immediately Access the code that you'll need for the challenge without having to type it all out yourself So anyway, let me just show you real quick how to access the code you're going to need basically what I've done here Is I've taken the code that I typed out previously this basic server code and I've created a new GitHub repo around it and Basically just pushed all that code up so you can see that there's core node server and this my file that TXT thing that we Used for testing purposes previously and if you click on this file you can see
1:17that it contains all that code Now what you're gonna need to do for this challenge is you're gonna want to open up a code space on this Specific commit right what's gonna happen is as I create more and more skills I'm gonna make changes to this to this repo so you're gonna need just this commit here So what you're gonna want to do is click on the commits list there may well be many more than just one commit by now and I'm going to click on the server built with no JS core This is the commit that you're going to need to access here in order to do this challenge And actually instead of clicking on that sorry You're gonna want to click over on this little button here that says browse the repository at this point in the history So if we click on this what that's going to do is Well, if you were seeing something different than what I had here you should be seeing the exact same thing right now with these two files and What you can actually do now is in order to open this up in a in an online editor code spaces That is you can just click on code and then you should see this button that says create code space on blah blah blah blah blah This this long commit hash right that's going to create a code space at this
2:37exact point in the project's history So that might run for just a minute and then you should see something that looks an awful lot like visual studio code Appear in your browser and you can personalize this a little bit as well in case the Bright color is hurting your eyes. You can always click on choose the look you want and then go to browse color themes And I believe the one that I usually use is um dark plus so there we go That's much easier on the eyes and oops. I didn't want that one Let's go back to there and select dark plus again accidentally click something Uh, so cool at this point you have visual studio code Basically a version of vs code running in your browser and you may want to zoom in a little bit as well There we go just to make it a little bit more readable and um What you can do now is because you have both core node server and my file that txt You can just run that directly in your terminal here By saying node core node server Dot js and then you'll see server is listening on port 3000 now an interesting thing is since this Visual studio code is Not running locally on your computer, right?
3:52It's running presumably on one of github servers Things might be a little bit different when you go to run the challenges Um, so what you can actually do it says your application is running on port 3000 And uh, if you say open in browser, what that's going to do is it's going to open a new browser window That will actually make a request to your server Uh, through proxy that github sets up, right? So you'll see hello from the no js server actually displayed in your browser Um, and really all that's happening here is your browser is Acting as the client instead of just running a piece of no js code in a terminal like we were doing before Now you can also test this thing out by the way Just by, um doing the same thing that we did before and that is opening up a new terminal and just saying node And then uh, you can say something like const hdtp equals require hdtp All right, and then you can say hdtp dot request and you can send that to hdtp colon slash slash local host 3000 And in order to uh, intercept the request and print it out to the console You can just say response dot pipe and send that along to process dot standard
5:08out And oops, let's uh make sure we type that correctly All right And then we can say dot end and sure enough we'll see that that has hello from the no js server printed out to the console as well So there's no difference if you're going to test it from here It's only if you're going to uh test it later on from outside this uh basic visual studio code refracing from outside this code space that you're going to need to Um, uh, worry about that and i'll walk you through that when we get there So i'm just going to close this other browser window there or browser, uh tab there for the time being And that's all you should need to know in order to actually uh run this challenge So let's actually get down to what this challenge is going to be And um in order to avoid confusion later, let's just stop our server and uh, we 'll clear out our browser Or clear out our other terminal as well What we did previously is we saw how to set up a very simple HTTP server that sent back Uh, a pretty useless message saying hello from the no js server So your challenge here is going to be to make this server do something a little bit more useful And send back the current date and time All right, so in other words You're going to write that logic inside this callback function
6:24And send back a string to whatever client sent the request Telling them what the date and time is and uh, in case you don't know how to do that in javascript Feel free to google that All right, so that's your basic challenges to rewrite our server here So that it will um, send back the date and time instead of just a, uh, useless string So, uh, that should take you maybe about a minute or two to complete this challenge And once you've made those changes definitely make sure that you actually test your server out So that's your challenge in the next video. I'll show you how I solved it So I hope this has been informative for you and I'd like to thank you for viewing [BLANK_AUDIO]
Challenge & Solution: Date and Time Server
0:00All right, so hopefully you've given this challenge a try. What I'm gonna do in this video is walk you through how I solve this challenge. Really, all that you had to do in this case was rewrite the logic that gets executed when the server receives a request so that instead of just printing something out to the console and sending a string back to the client, it actually got the current date and time and sent that back to the client. So what this looked like is we had to start off by getting the current date and time.
0:33And in JavaScript, the way that you do that is by saying new date rather, new date dot. And then you could either say to date string or to locale date string, which takes into account the locale that you're currently in and prints out the date in the format that you're used to seeing. So basically we could just take this and assign it to a variable. I said const, date and time equals new date dot to local date string.
1:03And in order to send this back to the client, all we had to do was say response dot end and in a back tick string here, which would make the date and time easier to insert. Nicely, we could say the current time is or the current date and time is. And then we could insert date and time like so. And oops, that needs a dollar sign before it.
1:34There we go. And that should be all we need to do in order to modify our server for that purpose. So let's try running this thing now and testing it out. What we're going to do is run our server with node core node server dot JS. And sure enough, we see server is listening on port 3000. This thing shows up down here as well. And we'll do things like make it public a little bit later on in the course. But let's open up another terminal. And actually, I did this in reverse here.
2:05We'll just open up whatever the other terminal is that we're not currently using. And we're going to run node inside of here. And then what we'll do is we'll say the same thing that we did before. We can just say const HTTP equals request. Oops, let's try that again. There we go. Not request require rather. HTTP. And then we can say HTTP dot request. And then we'll send it to local host. Oops, HTTP colon slash slash. That is local host 3000.
2:38And for the response, we'll want to print that out so that we can see it. We'll say response dot pipe and pipe that to process dot standard out. And then we can say dot end in order to send that request. So if we hit enter, sure enough, what we'll see is the current date and time is. And oops, that's actually just the date string. So let me get the time as well. And actually, what you can do for that is you can say, instead of too local date string, which just gives you the date, you can say to ISO string. And let's just save that and rerun our server here.
3:11We're going to stop it with control C and restart it. And let's go back now and try running that same command. You can actually just get up to it again, without having to type it all out by using the up arrow. And there we go. Sure enough, we see that we have this ISO string. It's a little bit hard to read. So if you wanted to play around with the formatting there as well, you could always do that. But we see that sure enough, 2023, 1125. And then we have the time here as well. So anyway, that is the basic solution to the challenge.
3:42Congratulations if you figured that out. And in case you're looking for other things to do, other things, other ways to practice this core node server functionality, I've left a list down below of other potential fairly simple projects that you could implement. So I hope this has been informative for you. And I'd like to thank you for viewing. [BLANK_AUDIO]
Frequently asked questions
What does the Get Started with Node.js Service Development lesson cover?
The lesson walks a team member through installing the LTS release of Node.js and NPM, confirming versions with node -v and npm -v, and switching versions with the Node Version Manager (NVM). It then covers running JavaScript snippets in the terminal, building a core Node.js web server with the built-in http module, and how multiple Node.js servers communicate with each other and with external clients to complete a request.
How long is this Node.js lesson and how does it fit into the JSNSD course?
The lesson runs 55 minutes and is the starting point of the OpenJS Node.js Services Developer (JSNSD) course, whose listed modules total more than 35 hours. Training Managers should plan the full course as a multi-week enablement path, with this lesson establishing the environment and core server basics that later skills build on.
What experience should a developer have before taking the JSNSD course?
The course is intermediate and fits software developers and backend engineers with existing JavaScript or Node.js experience, with catalog guidance pointing to roughly three to five years of backend JavaScript runtime experience. This first lesson starts from installation, so a team member can set up a consistent Node.js and NPM environment before moving into service development topics.
Why does the lesson build a Node.js server with the http module instead of a framework?
The trainer starts with a core Node.js server using only the built-in http module because the frameworks used in later skills are built on top of it. Seeing how a server listens for requests and sends responses without extra libraries gives developers a clear foundation before the course moves to frameworks that simplify server creation.
What is the hands-on challenge at the end of this Node.js lesson?
The team member builds and runs a date and time server in a GitHub Codespace, a browser-based version of Visual Studio Code, opened at a specific repository commit so the starting code matches the lesson. Running the core node server file in the Codespace terminal starts a server listening on port 3000, reinforcing the server basics covered earlier in the lesson.
Team training path
Turn this skill into assignable team training
This free skill is a preview of the courses your team can assign, track, and report on with CBT Nuggets.
Free training library
More free lessons from CBT Nuggets
JavaScript
Get Started with JavaScript
Free lesson from Learn NodeJS Fundamentals · 53m
JavaScript
Intermediate Promise Concepts
Free lesson from OpenJS Node.js Application Developer (JSNAD) · 48m
Microsoft Windows Server
Microsoft PowerShell 4 Foundations
Free lesson from PowerShell · 7h 57m
DevOps
Docker (v18.06.3)
Free lesson from Docker · 2h 6m
Networking
Border Gateway Protocol (BGP): Basic Operations
Free lesson from Border Gateway Protocol (BGP) · 60m
Networking
Define Wireless Network Architectures
Free lesson from Wireless Design Professional Training · 55m
$59
seat / mo, billed annually
