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