Express(Node.js) + Mysql (5) Rest API Delete Method

badasea·2022년 1월 11일
0

Node.js

목록 보기
5/6
post-thumbnail

Express(Node.js) + Mysql (5) Rest API Delete Method 호출하기

User Table

user_iduser_emailuser_nameuser_password
11bada1
2222
3333
모델
models/user.js
"use strict";

const mysql = require("../config/mysql");

let User = function (user) {
  this.user_id = user.user_id;
  this.user_email = user.user_email;
  this.user_name = user.user_name;
  this.user_password = user.user_password;
};
// 모든 사용자 검색
User.findAll = function (result) {
  mysql.query("Select * from user", function (err, res) {
    if (err) {
      console.log("error: ", err);
      result(null, err);
    } else {
      console.log("employees : ", res);
      result(null, res);
    }
  });
};
// 특정 사용자 검색
User.findById = function (id, result) {
  mysql.query("Select * from user where user_id = ? ", id, function (err, res) {
    if (err) {
      console.log("error: ", err);
      result(err, null);
    } else {
      result(null, res);
    }
  });
};
// 사용자 등록
User.create = function (newEmp, result) {
  mysql.query("INSERT INTO user set ?", newEmp, function (err, res) {
    if (err) {
      console.log("error: ", err);
      result(err, null);
    } else {
      console.log(res.insertId);
      result(null, res.insertId);
    }
  });
};
**추가**
// 사용자 삭제
User.delete = function (id, result) {
  mysql.query("DELETE FROM user WHERE user_id = ?", [id], function (err, res) {
    if (err) {
      console.log("error: ", err);
      result(null, err);
    } else {
      result(null, res);
    }
  });
};
module.exports = User;
컨트롤러
controller/user.controller.js
"use strict";
const User = require("../models/user");

exports.findAll = function (req, res) {
  User.findAll(function (err, user) {
    console.log("controller");
    if (err) res.send(err);
    console.log("res", user);
    res.send(user);
  });
};

exports.findById = function (req, res) {
  User.findById(req.params.id, function (err, user) {
    if (err) res.send(err);
    res.json(user);
  });
};
exports.create = function (req, res) {
  const new_user = new User(req.body);
  //handles null error
  if (req.body.constructor === Object && Object.keys(req.body).length === 0) {
    res
      .status(400)
      .send({ error: true, message: "Please provide all required field" });
  } else {
    User.create(new_user, function (err, user) {
      if (err) res.send(err);
      res.json({
        data: user,
      });
    });
  }
};
**추가**
exports.delete = function (req, res) {
  User.delete(req.params.id, function (err, user) {
    if (err) res.send(err);
    res.json({ message: "User successfully deleted" });
  });
};
라우터
routes/user.routes.js
const express = require("express");
const router = express.Router();

const UserController = require("../controllers/user.controller");

router.get("/", UserController.findAll);
router.get("/:id", UserController.findById);
router.post("/", UserController.create);
router.delete("/:id", UserController.delete);**추가**

module.exports = router;

Delete Method 호출 결과

Delete (delete)
http://localhost:3001/api/user/3
{"user_id":3}


GET (findAll)
http://localhost:3001/api/user
[{"user_id":1,"user_email":"1","user_name":"bada","user_password":"1"},{"user_id":2,"user_email":"2","user_name":"2","user_password":"2"}]
profile
🌊🌊Under the SEA🌊🌊

0개의 댓글