1 00:00:00,570 --> 00:00:05,450 In this video, I'll teach you how to setup an Express server from scratch. 2 00:00:05,450 --> 00:00:10,760 Here's the app it has one page and it says, I love Treehouse! 3 00:00:10,760 --> 00:00:12,940 And that's it believe it or not, 4 00:00:12,940 --> 00:00:18,280 this simple app contains all the basic components of an Express application. 5 00:00:19,380 --> 00:00:25,190 Express sets up its server, a server is a program that runs on a remote computer. 6 00:00:25,190 --> 00:00:30,300 Its job is to wait for HTTP requests from clients. 7 00:00:30,300 --> 00:00:32,390 Request is a technical term for 8 00:00:32,390 --> 00:00:36,640 what is happening when you type in a URL into a browser. 9 00:00:36,640 --> 00:00:43,250 Your browser is making a request to a server at the URL address you typed in. 10 00:00:43,250 --> 00:00:48,160 When a browser, or client, makes an HTTP request to the server, 11 00:00:48,160 --> 00:00:53,170 the service brings into action putting together a response. 12 00:00:53,170 --> 00:00:55,460 The development process requires you, 13 00:00:55,460 --> 00:00:59,000 the developer, to test and build a web application. 14 00:01:00,200 --> 00:01:05,510 It's usually much easier to run an application server on your local machine. 15 00:01:05,510 --> 00:01:10,450 This means you'll have both a server and a client on your computer. 16 00:01:10,450 --> 00:01:14,379 You'll use a browser to test the server in one window, for example. 17 00:01:14,379 --> 00:01:17,950 And then see the server respond in another. 18 00:01:19,090 --> 00:01:23,340 Later, when your ready to make your application available to the public. 19 00:01:23,340 --> 00:01:27,740 You'll need to deploy it to a remote server, where other people can visit it. 20 00:01:29,260 --> 00:01:32,981 Deploying a web application is beyond the scope of this course. 21 00:01:32,981 --> 00:01:36,030 But check the teacher's notes for more information. 22 00:01:37,030 --> 00:01:39,750 Let's set up the express server now. 23 00:01:39,750 --> 00:01:44,590 Here's my text editor with the code I left off in the last video. 24 00:01:44,590 --> 00:01:47,400 We have already required the Express module and 25 00:01:47,400 --> 00:01:50,180 assigned it to the variable Express. 26 00:01:50,180 --> 00:01:51,410 Let's create our app. 27 00:01:52,640 --> 00:01:57,060 To create an Express application we can just call express. 28 00:01:58,600 --> 00:02:02,010 The express function returns an Express application. 29 00:02:03,610 --> 00:02:07,840 Let's assign the Express application to a new variable called, app. 30 00:02:10,010 --> 00:02:13,818 This app is the central part of our application. 31 00:02:13,818 --> 00:02:18,300 We'll extend it throughout this course by adding routes, middleware, and 32 00:02:18,300 --> 00:02:19,880 other settings. 33 00:02:19,880 --> 00:02:23,040 The first thing we're going to do is setup the development server 34 00:02:23,040 --> 00:02:24,520 using the listen method. 35 00:02:25,600 --> 00:02:30,815 I can give this one parameter which is the port number 3000. 36 00:02:30,815 --> 00:02:35,490 Believe it or not, we have an Express app. 37 00:02:35,490 --> 00:02:41,040 This code will create a server, and when I run it, the server will run on my machine. 38 00:02:41,040 --> 00:02:45,365 And I can send it requests through a special URL called localhost. 39 00:02:46,550 --> 00:02:48,590 Let's see what this looks like. 40 00:02:48,590 --> 00:02:53,750 I could start the app by running node app.js. 41 00:02:53,750 --> 00:02:58,690 The prompt is gone because the server is busy listening for requests. 42 00:02:58,690 --> 00:03:02,638 Now in my browser, I can type localhost and 43 00:03:02,638 --> 00:03:07,257 then specify the port:3000 and hit Return. 44 00:03:07,257 --> 00:03:10,236 I can see an error message from Express. 45 00:03:10,236 --> 00:03:14,747 This is a good sign because it means that Express is running and 46 00:03:14,747 --> 00:03:16,759 it's sending a response. 47 00:03:16,759 --> 00:03:19,330 The error is coming from the fact that I 48 00:03:19,330 --> 00:03:23,070 have not told Express how to respond to any requests yet.