Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:12Length:1.7 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

1.2 Installing Express

In this lesson, we’ll briefly go over the things you need to set up in order to follow along with this course, and then we’ll write our first Express application!

Related Links

1.2 Installing Express

In this lesson we almost get to just jump right in and start looking at Express. But let's briefly go over two things that you need for writing not just Express-based applications, but Node applications in general. And the first probably goes without saying, we need Node. So if you already have Node installed, great. If it's an older version, then just update it. But if you don't, then you want to go to nodejs.org, and you want to download and install Node.js. Now, there are two versions. There is the LTS, which stands for long-term support, so this is going to be supported for several years out. And there is the current version, which has all of the latest and greatest features. But it's not going to be supported as long as the LTS version. And the way that the numbering scheme works is that the even-numbered versions are the LTS versions. So six, eight, ten, 12, 14 and so on, those are LTS. Now, here we see two even numbers, eight and ten. The reason why we see ten in the current column is because it just hasn't reached the point of becoming the long-term support version. It will be within a few months of this recording. So eventually, version ten will be the LTS, but until that time, just go with version eight. Or if you want to go with version ten or whatever the current version is, that's fine. But in most cases, I typically go with the LTS, so download and install Node.js, take all of the defaults and you will be good to go. So that's the first thing. The second thing you need is a code editor. Now code is just text. So technically, you just need a text editor. But a code editor, well, will be better. And there are many to choose from. And I don't want to twist your arm or anything. But I highly recommend that you use Visual Studio Code for JavaScript development. Because, well, it's just the best out there, at least as far as JavaScript development is concerned. So, feel free to use whatever tool that you want. I will be using Visual Studio Code, and you will find that many other people, not just in the Node community. But anyone working very heavily with Java Script will more than likely be using Visual Studio Code. It's just the best tool out there available. So, now that you have everything installed, we can actually get started with Express. So the first thing I'm going to do is make a new directory. I'm just going to call this first-express, and then we are going to run npm init -yes. This is going to initialize our project with all the default options. Now, if you leave -yes off, then it's going to ask you to enter values for each of one these items that you see on the screen. If you want to do that, feel free. I don't, so I just use -yes. And then I'm going to fire up my code editor. Now, we also need to install Express, and we're going to use npm to do that. So npm stands for Node Package Manager. If you're not familiar with Node at all, and this is the way that we manage our dependencies. So we're going to use npm to install Express, and we are going to save Express to our project. And if we look at our package.json file, which is what was actually created whenever we ran npm init. Eventually we're going to see dependencies, there it is, and we see express. So the installation should be done. It is. So right now, we have everything that we need to get started writing an application with Express. And we will do that by creating a new file. So let's create a new file. I'll call it app.js. And the first thing we want to do is pull in Express. So we are going to use require, express. And then this is going to give us a function called, express. This is the top level function that we would use to create our express app. So we will just create our app and we will call the express function. And then using our app, we will then set up our application. And one of the ways that we do that is by defining what we call our routes. So a route is basically, we define the URL, and then we define what happens whenever that URL is requested. So if we have a URL of just a slash, that is our homepage, essentially, that's the root of our application. So the first thing we pass to the get method is the URL that we want to essentially handle, basically. And then the second argument is a function that is going to have two arguments. The first is the request or an object representing the request that's coming to the application. And then an object that represents the response that we are going to work with to provide the response back to the browsers. So in this case, what we want to do is whenever we receive a request for the root of our application, then we just want to respond with the response object. We're going to use a method called send, so we are going to send a response, and then we get to specify what that response is. So in this case, we can just pass in some text, and we could say Hello, Express. So whenever we point our browser to just the root of our application, which will be local host, and we get to specify the port as well. But whenever we access that URL, then this is what we are going to see in the browser, the text Hello, Express. So we've just set up a route so that it looks like that something is going to happen. But then we need to tell our application that we want to listen for requests and we get to specify the port that we want. Now, for typical web applications, that would be port 80. But for most development environments, we use 8000 or 8080, or 8888. There's a variety of ports that we can use. And so, we are telling your application that we're going to listen for requests on port 8000, and then we have a callback function that wil execute, Whenever our application is successfully listening. So this would be a good time to write something to the console that says that we are listening on port something and something else. So let's do this, let's create a variable for our port. We'll set that to 8000, and we will use that variable as the first argument to the listen method. And we will also use it in our message here. So we'll just say that we are listening at http, and localhost at, and then our port. So we will include that, and there we go. So, now all we need to do is run this, and we will say node, and app.js. We see that our application is listening on localhost at 8000. So let's point our browser to that location. And then we will see our application actually work, and that's always a good thing to see. So, here we go. We are going to the root of our application, and we see Hello, Express. That is the exact text that we are outputting. And we could also output HTML if we wanted to. So if we wanted to change this to a b element, then we would have some bolded text. Now, we can't just go to the browser and refresh, unfortunately. We have to stop our running process, then we have to run it again. So, there's a little bit of headache in that, but we have tools available that will automatically do all that for us. But if we go back to the browser and refresh, then we are going to have bolded text. So whatever data we pass to this send method is being sent to the client, and that is exactly what we see in the browser. So there we go. We have our first express application, granted it's really nothing to be excited about, but you learned some very important things. First of all, how to create an Express application. You also learned about the get method that we use to define our routes for handling get requests. And then finally, you learned how to set your application up for listening for incoming requests at a specific port.

Back to the top