users.js 941 Bytes
Newer Older
CHAERIN KIM's avatar
CHAERIN KIM committed
1
2
3
4
const express = require('express');
const User = require('../schemas/user');
const bcrypt = require("bcrypt");
const { verifyToken } = require('./middlewares');
Kim, Chaerin's avatar
어헛    
Kim, Chaerin committed
5

CHAERIN KIM's avatar
CHAERIN KIM committed
6
7
8
const router = express.Router();

router.post('/', function (req, res, next) {
9
10
11
12
13
  console.log('/users post req.body', req.body)
  User.findOne({ id: req.body.id }, function (err, users) {
    if (err) return res.status(500).json({ error: err });

    if (users) {
14
      return res.status(404).json({ error: '이미 존재하는 학번입니다.' })
15
16
17
18
19
20
    };

    const user = new User({
      name: req.body.name,
      id: req.body.id,
      password: req.body.password,
21
22
      question: req.body.question,
      answer: req.body.answer,
23
24
25
26
27
28
29
30
31
32
33
34
    });

    user.save()
      .then((result) => {
        console.log(result);
        res.status(201).json(result);
      })
      .catch((err) => {
        console.error(err);
        next(err);
      });
  })
Kim, Chaerin's avatar
어헛    
Kim, Chaerin committed
35
});
36
37


Kim, Chaerin's avatar
어헛    
Kim, Chaerin committed
38
module.exports = router;