03 - Express code
In this module we'll make a simple Express server.
File Structure
Let's go inside of app.js
in your server
directory.
Basic Server Code
In app.js
, go ahead and add the following code:
Analysis
Let's analyze the code:
1. Here we require the use of the express
npm package that we've installed in our dependencies.
2. We create an instance of express. We're actually firing off a top-level express()
function, a function exported by the Express module. This allows us to create an Express app.
3. app.listen
will use express to start a UNIX socket and listen for connections on the given path. This method is identical to Node’s http.Server.listen()
.
4. The given path is localhost:3000
.
5. We call a callback function when the connection happens with a simple console.log
.
Running the Code
Open your terminal in VS Code. Make sure you are in the server directory.
Type in
node app.js
.In your console/terminal window, you should see your console message:
Don't worry about the error message right now. It won't affect anything.
We'll explain this particular screenshot later.
Last updated