AllenYuan

profiles api v1

May 9th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var router = require('express').Router();
  2. var mongoose = require('mongoose');
  3. var User = mongoose.model('User');
  4. var auth = require('../auth');
  5.  
  6. // if the route contains the username path parameter, fetch the user's profile
  7. router.param('username', function(req, res, next, username) {
  8.   // find the user with the username from the path
  9.   User.findOne({username: username}).then(function(user){
  10.     if (!user) { return res.sendStatus(404); }
  11.  
  12.     req.profile = user;
  13.  
  14.     return next();
  15.   }).catch(next);
  16. });
  17.  
  18. router.get('/:username', auth.optional, function(req, res, next) {
  19.   return res.json({profile: req.profile.toProfileJSONFor()});
  20. });
  21.  
  22. module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment