SHARE
TWEET

users.js

a guest Sep 20th, 2019 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. var router = express.Router();
  3.  
  4. var userList = [];
  5.  
  6. //Returns userList
  7. router.get('/', function(req, res, next) {
  8.  
  9.     res.status(200);
  10.     res.json(userList);
  11.  
  12. });
  13.  
  14. //Adds user to userList and returns index of user and user object
  15. router.post('/', function(req, res, next) {
  16.  
  17.     var userObject = req.body;
  18.     var i = userList.push(userObject) - 1;
  19.     var response = {index: i, user: userObject};
  20.    
  21.     res.status(200);
  22.     res.json(response);
  23.  
  24. });
  25.  
  26. //Deletes user in userList at given index and returns updated userList
  27. router.delete('/:index', function(req, res, next) {
  28.  
  29.     var index = req.params.index;
  30.     userList.splice(index, 1);
  31.    
  32.     res.status(200);
  33.     res.json(userList);
  34.    
  35.  
  36. });
  37.  
  38. //Modifies user in userList at given index and returns index of updated user and updated user object
  39. router.patch('/:index', function(req, res, next) {
  40.  
  41.     var index = req.params.index;
  42.     var userObject = req.body;
  43.     userList[index] = userObject;
  44.    
  45.     var response = {index: index, user: userObject};
  46.    
  47.     res.status(200);
  48.     res.json(response);
  49.    
  50.  
  51. });
  52.  
  53. module.exports = router;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top