Mongo / Express无法加载路由(邮递员中出现意外的“ <”)

时间:2018-07-10 13:35:09

标签: node.js mongodb express routes

尝试使用User.findById通过参数从ID获取用户用户对象,并且无法访问此路由。

邮递员响应:

Cannot GET /users/get/
,状态为404(未找到),状态为奇怪。 邮递员JSON响应: 意外的'<'

曾经使用express-promise-router,但也尝试使用默认的express.Router();

这是我的代码:

routes / users.js

driver.execute_script("chrome.cookies.set({url:'https://exxample.com', name: 'myName', value: 'someValue'});")

控制器/用户

const express = require('express');
const router = require('express-promise-router')();
const passport = require('passport');
const router1 = express.Router();
require('../passport');

const { validateBody, schemas } = require('../helpers/routeHelpers');
const UsersController = require('../controllers/users');

const passportSignIn = passport.authenticate('local', { session: false });
const passportJWT = passport.authenticate('jwt', { session: false });

router.route('/signup')
  .post(validateBody(schemas.authSchema), UsersController.signUp);


router.route('/signin')
  .post(validateBody(schemas.authSchema), passportSignIn, UsersController.signIn);

router.route('/get/:id')
  .get(UsersController.getUser);


router.route('/secret')
  .get(passportJWT, UsersController.secret);

server.js

module.exports = router;
const JWT = require('jsonwebtoken');
const User = require('../models/user');
const { JWT_SECRET } = require('../configuration');

const signToken = (user) => {
  return JWT.sign({
    iss: 'CodeWorkr',
    sub: user.id,
    iat: new Date().getTime(), // current time
    exp: new Date().setDate(new Date().getDate() + 1) // current time + 1 day ahead
  }, JWT_SECRET);
};

module.exports = {
  signUp: async (req, res) => {
    const { email, password } = req.value.body;

    // Check if there is a user with the same email
    const foundUser = await User.findOne({ 'local.email': email });
    if (foundUser) {
      return res.status(403).json({ error: 'Email is already in use' });
    }
    // Create a new user
    const newUser = new User({
      method: 'local',
      local: {
        email: email,
        password: password
      }
    });

    await newUser.save();

    // Generate the token
    const token = signToken(newUser);
    // Respond with token
    return res.status(200).json({ token });
  },
  signIn: async (req, res) => {
    // Generate token
    const token = signToken(req.user);
    res.status(200).json({ token });
  }, 
  getUser: async (req, res) => {
    User.findById(req.params.id)
      .then((user) => {
        res.status(200).json({ user });
        console.log('test');
      });
  },

  secret: async (req, res) => {
    console.log('I managed to get here!');
    res.json({ secret: 'resource' });
  }
};

其他发布路线效果很好

0 个答案:

没有答案