---
title: "[NJS] Routing"
slug: njs-routing
section: tech
date: 2016-07-17T19:12:00.000Z
canonical: https://roland.leth.ro/blog/tech/njs-routing
---

Express offers a really easy routing system, which is the main selling point:

```js
var app = require('express')()
app.use('/', require('./routes/routes'))
```

This will delegate all routing to the `routes.js` file, inside the `routes` folder, at the root level. Here, we delegate individual routes to their specific files, but declaring routes will be just as easy:

```js
var router = require('express').Router()

router.use('/privacy-policy', require('./privacy-policy'))
router.use('*', function(req, res) { // req stands for request, res for response
  res.render('not-found') // This is the 404 page, and should be the last route
})

module.exports = router
```

I picked the privacy policy route as an example, because you can download the policy in `md` format, so we'll cover nested routes and serving files in one go:

```js
var router = require('express').Router()

router.get('/', function(req, res) {
  res.render('privacy-policy')
})

router.get('/download', function(req, res) {
  res.download('./assets/files/Privacy Policy.md', 'Roland Leth Privacy Policy.md')
})

module.exports = router
```

Because we got here from `router.use('/privacy-policy)`, `/` just means *the root of the current route*, which will resolve to `rolandleth.com/privacy-policy`, the second `get` resolving to `rolandleth.com/privacy-policy/download`, respectively.

The `res.download`'s first parameter is the file to server, and the second parameter is the name that the resource will be saved with.

Next time we'll talk about layouts.