Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Often when starting an Express project, it makes sense to use express-generator to create a starting place for us. But for our purposes this would create a lot of unnecessary files we would have to then delete. That’s because express-generator builds in a lot of user interface-based files like templates and static files (HTML, CSS or client-side JS), which REST services have nothing to do with, being solely data-based. We’ll instead work the other way, and create our app from scratch, which it turns out is not actually too hard to do! Let’s get started.
-
0:04
It's time to start coding.
-
0:06
If you've used express in a project before,
-
0:09
you may have used express Generator to create boilerplate code.
-
0:14
express generates or provides starter code to set static HTML, CSS and image files.
-
0:20
Since our API will only send JSON,
-
0:23
the extra express generated code isn't required.
-
0:27
It's easier for us to start from scratch.
-
0:30
To build our API, we'll be using a command line terminal and a text editor.
-
0:36
It doesn't matter which text editor you use, but I'll be using sublime text.
-
0:43
Let's start a terminal window.
-
0:46
If you haven't created a folder where you want to build this project,
-
0:50
create that now.
-
0:51
It doesn't matter what you call it.
-
0:54
I'm going to call mine qa-rest-api.
-
1:02
Change into that directory and run npm
-
1:07
init --yes.
-
1:11
This accepts all defaults and creates the package.json.
-
1:17
It's a fast way to create a basic package.json file.
-
1:21
Thoughtful configuration of a package.json file is usually a great idea,
-
1:26
and there's a Treehouse course on using MPM that covers this in depth.
-
1:31
But for our purposes this default configuration will be fine.
-
1:37
Next, let's install express by
-
1:42
running npm install express@ tilde,
-
1:48
which means any patch release,
-
1:54
4.13-- save.
-
2:05
Once express is installed, switch over to your text editor and
-
2:09
create a new file, app.js.
-
2:24
This will be our apps main file.
-
2:27
Let's adjust the package.json file to reflect that.
-
2:36
At the top of the app.js file we will type use strict,
-
2:42
to help us write better code.
-
2:49
If you'd like to know more about this, Treehouse has a workshop
-
2:52
on this statement, you can find the link in the teacher's notes below.
-
2:56
Then we'll bring in the express
-
3:02
module with var express is
-
3:07
equal to require express.
-
3:12
Let's use the express function to return an instance of our application.
-
3:18
We can store this in the variable app.
-
3:27
The application object is stored in the app variable.
-
3:31
This will let just control how the application behaves,
-
3:34
when restful requests are made to it.
-
3:36
We'll configure it shortly.
-
3:39
Right now, let's add a couple more lines, to get our app into a working state.
-
3:45
First, we'll need to specify a port to serve the app on.
-
3:50
Var port is equal to
-
3:55
process.env.PORT or
-
4:00
3000.
-
4:03
This expression will evaluate to 3000 unless we're deploying the out to
-
4:07
a production environment.
-
4:09
We won't need to worry about any of this in this course, though.
-
4:13
For our purposes, this will set the port to 3000.
-
4:17
We can start the server by calling app.listen, and
-
4:20
passing in the port to listen on.
-
4:29
If we started the app now, it would work.
-
4:33
But you wouldn't be able to see anything in the browser.
-
4:37
We haven't set up any routes.
-
4:40
You'd only know the sever was running,
-
4:42
because there'd be no error in the console.
-
4:46
Let's give ourselves some feedback.
-
4:48
App.listen also accepts a call back function as its second parameter,
-
4:53
which will be called once the server has begun to listen for requests.
-
5:08
Let's provide a callback that will lock some text to the console once the server
-
5:13
has successfully set up.
-
5:17
Express server is listening on port and
-
5:24
then the port variable.
-
5:31
Let's save this file and switch back to the terminal to start the app.
-
5:39
There is the message, letting us know that the server is active,
-
5:44
not much to see yet but it's a good start.
-
5:48
We'll build the Express app up in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up