DEV Community

Cover image for Express RESTful Routing
Matthew Palmer
Matthew Palmer

Posted on

Express RESTful Routing

Introduction

Previously, I wrote a blog about how to get started with the Express-Generator. If you're trying to get set up in Express, you can find my tutorial here. In this blog, we're going to piggyback off my last Express blog and come to understand how the routing works.

Resources

  • I highly recommend that you download The Postman App. It's really great for testing HTTP requests without building testing forms and populating your database with faux data. (i.e. my_user_test@email.com + somepassword1234) Best part is, it works with any library/framework. It tests those HTTP requests while totally language agnostic to the technology handling the logic.

HTTP Requests

If you've ever handled requests before, you're familiar with the basic requests such as GET, POST, PUT, and DELETE.

HTTP Requests Example Meaning
GET /posts Meant to GET or show a user's post
POST /posts Meant to POST information to a database. In this case, creating a new post
PUT /posts/:id/edit Meant to PUT, or update information. In this case, updating a user's post by the ID in a database.
DELETE /posts/:id Meant to DELETE information in a database. In this case, deleting a user's post.

There are a total of 7 HTTP Requests, GET, POST, PUT, HEAD, DELETE, PATCH and OPTIONS. We will only be covering four of them.

Routes

Under your routes, create a file posts.js. Populate it with this code:

const express = require('express');
const router = express.Router();

/* GET posts index /posts */
router.get('/', (req, res, next) => {
  res.send('INDEX /posts');
});

/* GET posts new /posts/new */
router.get('/new', (req, res, next) => {
  res.send('NEW /posts/new');
});

/* POST posts create /posts */
router.post('/', (req, res, next) => {
  res.send('CREATE /posts');
});

/* GET posts show /posts/:id */
router.get('/:id', (req, res, next) => {
  res.send('SHOW /posts/:id');
});

/* GET posts edit /posts/:id/edit */
router.get('/:id/edit', (req, res, next) => {
  res.send('EDIT /posts/:id/edit');
});

/* PUT posts update /posts/:id */
router.put('/:id', (req, res, next) => {
  res.send('UPDATE /posts/:id');
});

/* DELETE posts destroy /posts/:id */
router.delete('/:id', (req, res, next) => {
  res.send('DELETE /posts/:id');
});


module.exports = router;
Enter fullscreen mode Exit fullscreen mode

Now, inside of app.js, apply this line between lines 5 and 11:

const posts = require('./routes/posts');
Enter fullscreen mode Exit fullscreen mode

And then this line between lines 21 and 27:

app.use('/posts', posts);
Enter fullscreen mode Exit fullscreen mode

This is where The Postman App will come in handy. If you came from my first blog covering the setup of this application, you can start up your server by typing nodemon into your terminal. Using The Postman App, we can begin to test each route to be sure they are working correctly. Your response from each route will be exactly what is inside of res.send('string-information-here') with each route/HTTP request delivering their own response.

Is this the right setup for all route files?

You might think so at first, but it's important to understand that not everything in a database necessarily needs a particular action. One example would be when you're writing a review. Does it make sense to create an entire route just for a review form?

Doesn't it make better sense to attach that form in the same route as a user's post you might be viewing? Depending on the type of application you've got, you might not even want a user to delete their own review (for whatever reason). Point is, the type of routing you set up is highly dependent on the desired behavior of your application. There is no amount of information I could write here that would solidify these concepts better than if you were to write up your own code and practice narrow use cases.

Conclusion

RESTful Routing follows roughly the same pattern when it comes to MVC frameworks. Coming from a Ruby on Rails background, Express seems to be no exception. Understanding the MVC architecture is paramount as a transferrable skill. Stay tuned, because I will be continuing to write more blogs about Express in the near future. 😉

Please leave a comment if you have any questions! I'll be happy to answer.

Follow Me!

LinkedIn/MatthewPalmer9
Github/MatthewPalmer9
Twitter @MattPDev

Top comments (0)