Skip to content

mohllal/users-list-api

Repository files navigation

Users Listing API

Develop an API endpoint to get a list of users.

Requirements

  • Pagination support
  • Filtration support
  • Cache-Control (client and server-side)

Commands

npm run dev # run the API in development mode
npm run test # test using Jest
npm run coverage # test and open the coverage report in the browser
npm run lint # lint using ESLint
npm run docs # generate API docs

Playing locally

Run the server in development mode.

npm install
npm run dev
Express server listening on http://0.0.0.0:9000, in development mode

Playing with Docker Compose

To run the application locally, you can use the Docker Compose tool.

docker-compose -f docker-compose.local.yaml up
Express server listening on http://0.0.0.0:9000, in compose mode

And to run the tests locally using Docker Compose.

docker-compose -f docker-compose.test.yaml up

API Docs

You can find the API documentation in the DOCS.md file. This file is autogenerated by the apiDoc inline APIs documentation tool.

Cache-Control

Cache-Control is set to public, max-age=300 in the response headers. This means that the response will be cached for 300 seconds.

You can find Cache-Control directives configured in the express service file.

app.use(cacheController({ public: true, maxAge: 300 }))

Server-Side Caching

Server-Side caching is enabled by default and configured in the middlewares file. The response will be cached for 300 seconds.

const cache = (duration) => {
  return async (req, res, next) => {
    const key = '__express__' + req.originalUrl || req.url

    const cached = await ioredis.get(key)
    if (cached) {
      const json = JSON.parse(cached)
      return success(res)(json)
    } else {
      res.sendResponse = res.send
      res.send = (body) => {
        ioredis.set(key, body, 'EX', duration * 1000)
        res.sendResponse(body)
      }
      return next()
    }
  }
}

Heroku Deployment

This application is deployed to Heroku via this link.