Autoplay

    Create a Basic Server with Express

    Ben ClinkinbeardBen Clinkinbeard

    In this lesson we will initialize our Express application using npm init.

    After loading express through npm we will look at a couple basic functions Express exposes to us such as .get and .send. This will allow basic interaction via the url.

    We will also see how using a package like nodemon can allow for rapid iteration when developing an application.

    expressExpress
    nodeNode.js
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 Since Express is a Node program, we're going to start by initializing a package JSON with our default values by just using MPMinit-y.

    00:08 We can install the Express and we use the -S flag so that it gets saved to our package.json, and we have that dependency recorded. We're going to create an index.js file, and then we will go look at that and see how we initialize our Express application.

    00:28 If we require Express here, that's going to actually give us a function reference. To create our app instance, we're just going to call that function Express with a parentheses, and now we have an instance of an Express app.

    00:46 The most basic call here is going to be App.Get, with just a slash, so that's essentially saying, "Express, when you get an HTTP Get request to the root path, we want you to call this function that we are going to give you."

    01:02 You'll see that the two arguments that we pass in are Req and Res, or Request and Result. Basically, to send data back to the browser or whoever is calling this, we're just going to say Result.Send, "Hello World," and then we're going to start our application by telling it to run on port 3,000.

    01:24 If we now go back to our terminal and start up index.js, we can see that we can go in our browser, load up localhost 3,000 and we do in fact get, "Hello World" returned. It would be nice to have a little bit of feedback in the terminal, usually, when you start that server so that you know what's going on.

    01:42 We are going to create a server variable, and then we're going to give it a callback function when we call, listen so that we can know when that server is starting up. In this case, we are going to just log out a message saying, "Hey, there's a server running at..." whatever port it is that we are starting up on.

    02:01 If we save that, and then go back to our terminal, we can kill the server that was still running, and then restart it, and we now see our message that tells us that our server is in fact running on port 3,000.

    02:14 One thing that is a best practice or at least a convention in the node ecosystem is that, when you're dealing with a server application, you're generally going to want to create a MPM start command that will start up your server.

    02:28 That way, people don't have to know which file it is that gets used, any possible arguments that are involved. We are going to create an MPM start script here in our package.JSON, and now we can call it MPM start. You can see that it is in fact just calling node index.js, and that gets our server running just the same as before.

    02:49 We can go ahead and kill that, and you will notice that every time we make a change, and we want to restart things that we have to kill that server. During development, that can get really annoying, really fast. What we're going to do is we're going to install a package called NodeMon, which will essentially lead us set up a file watch so that when any of the files in our project change, it's going to restart that server.

    03:17 While that's installing, we're going to set up a dev MPM script that just runs NodeMon and passes it the index.js file. Now we can run MPM run dev, and it's going to run that NodeMon call and start up our server. You can now see that if we go and change our file, and save that, you get the server restarting automatically in the terminal.

    03:43 This makes it a whole lot easier to work and be productive as you are trying things out, and developing. Now, we can go back and make changes at will, and not have to worry about restarting that server in between each time. And so, just to show the next most basic thing in Express, we can tell it, when you get a HTTP Get request to the /yo path, we want you to call this handler instead of the other one, and we're just going to send back, "Yo."

    04:16 Our server restarted automatically for us. We can go try out that URL, and so we do in fact get a different response.

    Discuss

    Discuss