Set up Express to Automatically Reload on Save with nodemon

Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

In this lesson, we set-up an Express application and have it send a test response. We then test that the server is working using an API client, in this case Insomnia. We'll also install and setup nodemon so our server will reload every time our files change making development much faster.

Kevin Cunningham: [0:00] My api folder just contains a README. First of all, I will open a new terminal window. I'll change directory and I will use yarn to initialize the package.

[0:14] I'm using Express to build my API, so first of all, I'll install that with npm. Then, I will create an entry file for my API, in this case, I'll call it server.js.

[0:27] Inside my server file, I'm going to import express. I'm then going to create an app, which is going to use express. Then, my app is going to use just the bare root. It's going to have a function that will accept the request and the response, and respond just with a text snippet saying, "this is working."

[0:52] Next, I'll have my server listen on port 3001, because my next application is listening on port 3000. I will log to the console once the server has started, to tell me where it's listening. Let's use node to start our server and test it if it's working.

[1:12] We've successfully logged to the console, which is great. Now, to test the API, I'll use a client, in this case insomnia localhost:3001. I'll send a request and I got a response with a text snippet, "this is working."

[1:24] While the server is running, if Alt-D at the code and then trigger a request to the API, we'll see that there's no change. We need to restart the server after every change we make to the code.

[1:39] To stop as having to do that, I'm going to use a package called nodemon, which restarts the server every time that there's a change to the files. I'll add a scripts block to my package.json file. I will add a dev script, in this case, it's nodemon server.js.

[1:59] Back in my terminal, I'll run npm run dev. Then, I'll see that my server's still saying this is still working. Let's test on insomnia and then let's try updating the text without restarting the server.

[2:13] You see, when I save this file, nodemon will automatically restart the server, so when I get back to my insomnia client, I'll get the response I wanted.

egghead
egghead
~ 14 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