
Lesson Description
The "Create an Express Application" Lesson is part of the full, API Design in Node.js, v5 course featured in this preview video. Here's what you'd learn in this lesson:
Scott explains how to create a server with Express in a Node.js project, stressing proper source setup and dependency installation. He demonstrates setting up routes, handling requests, running the server on a port, and using logging to monitor activity.
Transcript from the "Create an Express Application" Lesson
[00:00:00]
>> Speaker 1: Let's do it So, main branch: you can stay on that branch, create your own branch You do not have to create the branches that I'm creating That's just for me to stay consistent with the lessons that I planned out
[00:00:00]
This is me trying to stay on track and not get off track So yeah, fill whatever branch you want as long as it's off of the main branch So first thing is let's create a server, right
[00:00:00]
I mean, that seems appropriate So let's do that So what we want to do, if you don't already have one, you want to make a source directory here, src Why do we have a source directory
[00:00:00]
Does it matter No, it doesn't matter This is not like Next.js where there are file-based conventions and names matter This is just a convention that people use typically
[00:00:00]
In most projects, you might see something like this, you might see source, you might see app, and the reason you would see something like that is because there is an opposite of this, which might be like dist, which would be the output of the source if it had to be built or it might be output or something like that
[00:00:00]
Source just means these are the raw source files that we're going to be writing, so src is short for source, but you can put whatever you want in another project For this project, we're going to use source
[00:00:00]
And then what we're going to do is we're going to make a server.ts OK Did I do npm install I think I did Double-check, yeah, cool When you pull down someone's project on GitHub and it's another project, you always do npm install
[00:00:00]
When you update your Git branch with a rebase or a merge locally, you always do an npm install Whenever you get new code on your branch, you always do an npm install
[00:00:00]
Just remember that I'm going to walk you through Express We're just going to make a Hello World, health check, simple thing here So first thing is let's import I'll make this bigger
[00:00:00]
Import Express from 'express' - again, if you don't know what this crazy syntax is that I wrote, going back to the notes, this is ESM modules It works the same way in TypeScript as it does in ESM
[00:00:00]
If you want more deep dive, go check out the intro to Node.js The way that Express exposes its interfaces, typically the way that we do this is we create an app - you can call it server, you can call it whatever you want, but the convention is we call it an app
[00:00:00]
We just invoke the Express function and we go from there So I'm going to say app equals Express like that As I was saying earlier, a server is a router that routes URLs to code, so we need to define those URLs
[00:00:00]
There's also a missing piece because it's not just URLs Sometimes there's also something called a verb, an HTTP verb, and we'll talk more about those soon A verb is essentially exactly what it sounds like: it's like some type of action that you want to take, that you want to indicate to the system that you want to do
[00:00:00]
So in this case, I want to register a GET request I'll say app.get So when someone does a GET request to this app, and right now I'll just say for the route of /health
[00:00:00]
This is a common convention in servers is to make like a /health route that you can hit at any point to see if your server is still alive So you might see something like on an interval just pinging /health on some interval to make sure your server is not dead
[00:00:00]
[Note: The full transcript has been corrected following the specified guidelines The entire text would continue in this manner, maintaining technical accuracy, correcting speech patterns, and preserving the original meaning.]
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops