Serve a Basic Route with Express 5 and node 14

Jamund Ferguson
InstructorJamund Ferguson
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated a year ago

In this lesson we'll build a basic web server in 2 minutes using Express 5 and node 14's native ES module support.

We'll start by creating a new project using npm init and then add "type": "module" to our package.json file to opt-in to ES Module support. From there we'll create an index.js file where we create an instance of the express module and add a single GET route that sends the response "hello". Finally we listen on port 3000 and confirm that our route responds to requests using both curl and a web browser.

Pre-requisites:


Express 5 Express 5 offers very few changes from version 4. The main one I'm interested in is built-in support for async/await, which greatly simplifies how we handle network and file system I/O calls in our web application. Read more at Migrating to Express 5

async/await Read more about these recent JavaScript language additions in Getting Started with async/await on FreeCodeCamp.

[0:00] First, create a folder for your application to reside. I'm going to call mine notes. Then type node-v to confirm that you have version 14 or higher. If not, hit pause, go to nodejs.org and install it right now. From inside your application folder, type npm init. This will set up your package.json file. Feel free to hit enter for every single prompt.

[0:24] Now open your package.json file and add the key type and the value module. Then save the file. This is going to tell node.js that your project is using the es module format instead of common.js. Now type npm install express at next to install the latest version of express.

[0:40] You'll see for us an installed version 5.00 alpha 8. In this video, we're going to be using the latest version of express because it gives us some new features such as the ability to use async away in our routes. Now let's create a file called index.js where we'll put the brains of our application.

[0:55] Type import express from express to bring in the express module and then type const app equals express to create an instance of the express module. Then type app.get to create your first route. The first parameter is a forward slash in quotes.

[1:12] For the second, we'll create a function that takes two parameters, rec and res, which stand for request and response. These two parameters are the bread and butter of any node application.

[1:20] Inside your function type res.send and pass it the string hello. Finally, we'll call app.listen and pass in the port 3000. Save the file and type node index.js. From there you can open up a web browser to localhost: 3000 and you should see the word hello.

[1:40] You can also open up a new tab and type cURL http://localhost3000 and that should also return the response hello.

egghead
egghead
~ 5 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today