Learn the basics of setting up a server with Express.
In this video, I'll teach you how to setup an Express server from scratch. 0:00 Here's the app it has one page and it says, I love Treehouse! 0:05 And that's it believe it or not, 0:10 this simple app contains all the basic components of an Express application. 0:12 Express sets up its server, a server is a program that runs on a remote computer. 0:19 Its job is to wait for HTTP requests from clients. 0:25 Request is a technical term for 0:30 what is happening when you type in a URL into a browser. 0:32 Your browser is making a request to a server at the URL address you typed in. 0:36 When a browser, or client, makes an HTTP request to the server, 0:43 the service brings into action putting together a response. 0:48 The development process requires you, 0:53 the developer, to test and build a web application. 0:55 It's usually much easier to run an application server on your local machine. 1:00 This means you'll have both a server and a client on your computer. 1:05 You'll use a browser to test the server in one window, for example. 1:10 And then see the server respond in another. 1:14 Later, when your ready to make your application available to the public. 1:19 You'll need to deploy it to a remote server, where other people can visit it. 1:23 Deploying a web application is beyond the scope of this course. 1:29 But check the teacher's notes for more information. 1:32 Let's set up the express server now. 1:37 Here's my text editor with the code I left off in the last video. 1:39 We have already required the Express module and 1:44 assigned it to the variable Express. 1:47 Let's create our app. 1:50 To create an Express application we can just call express. 1:52 The express function returns an Express application. 1:58 Let's assign the Express application to a new variable called, app. 2:03 This app is the central part of our application. 2:10 We'll extend it throughout this course by adding routes, middleware, and 2:13 other settings. 2:18 The first thing we're going to do is setup the development server 2:19 using the listen method. 2:23 I can give this one parameter which is the port number 3000. 2:25 Believe it or not, we have an Express app. 2:30 This code will create a server, and when I run it, the server will run on my machine. 2:35 And I can send it requests through a special URL called localhost. 2:41 Let's see what this looks like. 2:46 I could start the app by running node app.js. 2:48 The prompt is gone because the server is busy listening for requests. 2:53 Now in my browser, I can type localhost and 2:58 then specify the port:3000 and hit Return. 3:02 I can see an error message from Express. 3:07 This is a good sign because it means that Express is running and 3:10 it's sending a response. 3:14 The error is coming from the fact that I 3:16 have not told Express how to respond to any requests yet. 3:19
You need to sign up for Treehouse in order to download course files.Sign up