You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
628 B

5 years ago
  1. const fs = require('fs')
  2. const express = require('express')
  3. const router = express.Router()
  4. const db = JSON.parse(fs.readFileSync('./api/db/users.json', 'utf8'))
  5. // Get user list.
  6. router.get('/', (req, res) => {
  7. const q = req.query.q
  8. if (q) {
  9. res.json(db.filter(item => item.username.toLowerCase().includes(q.toLowerCase())))
  10. } else {
  11. res.json(db)
  12. }
  13. })
  14. // Get a user.
  15. router.get('/:userId', (req, res) => {
  16. const user = db.find(item => item.id === parseInt(req.params.userId, 10))
  17. if (user) {
  18. res.json(user)
  19. } else {
  20. res.status(404).json({ detail: 'Not found.' })
  21. }
  22. })
  23. module.exports = router