Avoid manually prepending '/api' to every Express route with this simple method
Tags: express, node, javascript
Published: 2018-10-23 11:32:33

---
title: Avoid manually prepending '/api' to every Express route with this simple method
published: true
canonical_url: https://www.coreycleary.me/avoid-manually-prepending-api-to-every-express-route-with-this-simple-method/
description: 
tags: express,node,javascript
---
<em><strong>Originally published at <a href="https://www.coreycleary.me/avoid-manually-prepending-api-to-every-express-route-with-this-simple-method/">coreycleary.me</a></strong>. This is a cross-post from my content blog. I publish new content every week or two, and you can <a href="https://www.coreycleary.me/about/">sign up to my newsletter</a> if you'd like to receive my articles directly to your inbox! I also regularly send cheatsheets, links to great tutorials by other developers, and other freebies!</em>

Have you ever been frustrated by having to always <strong><em>manually</em></strong> add <strong>'/api'</strong> to the beginning of each of your Express routes? Usually I see this come in two forms:

<strong>Form 1:</strong>
In `app.js` file:
```javascript
app.use('/api/users', require('./routes/users'))
app.use('/api/posts', require('./routes/posts'))
app.use('/api/comments', require('./routes/comments'))
app.use('/api/subscriptions', require('./routes/subscriptions'))
```
<strong>Form 2:</strong>
In routes file:
```javascript
router.post('/api/users', users.createUser)
router.post('/api/posts', blogpost.postBlogpost)
router.post('/api/comments', comment.postComment)
router.post('/api/subscriptions', subscription.addSubscription)
```

Either way- it looks messy, you have to remember to add it each time if you aren't copying/pasting, and it seems like there should be a better way of doing it.

<h3>Let's clean it up</h3>
Fortunately, the fix for this is really simple.

In  our routes file, remove '/api' from each route
```javascript
const express = require('express')
const router = express.Router()

router.post('/users', users.createUser)
router.post('/posts', blogpost.postBlogpost)
router.post('/comments', comment.postComment)
router.post('/subscriptions', subscription.addSubscription)

module.exports = router
```

And in `app.js` (or `server.js`, wherever you setup your Express server):
```javascript
const express = require('express')
const app = express()
const routes = require('./routes')

app.use('/api', routes) // routes is our routes file above
```

So instead of prepending '/api' manually to each route, we import the Express router into `app.js` and prepend '/api' - this time, only once! - to `app.use()`

<h3>How does this work?</h3>
What we've essentially done here is composed a pipeline.

Let's consider `app` as our main app and `router` (our routes file) as a "sub-app".

When our application receives a web request, it will hit `app.use('/api', routes)` first. If the request was sent to an endpoint matching something like '/api/blah/blah' it will <strong>match</strong> on `app.use('/api', routes)` and from there, get routed to our sub-app router, which contains the rest of our routes.

So Express builds out this pipeline for you, and you don't have to manually add '/api' to each route anymore!

<h3>One last thing!</h3>

I'm writing a lot of new content to help make Node and JavaScript easier to understand. Easier, because I don't think it needs to be as complex as it is sometimes. If you enjoyed this post and found it helpful <a href="https://www.coreycleary.me/about/">here's that link again</a> to subscribe to my newsletter!