Express(Node.js) + Mysql (3) MVC
Sever.js 에서 CRUD를 위한 쿼리문을 써서 하드 코딩 하기 보단
Model - View - Controller 를 구분하여 모듈화
├── Express
│ ├── config
│ │ └── mysql.js
│ ├── Model
│ │ └─── user.js
│ ├── Controller
│ │ └─── user.controller.js
│ ├── Routes
│ │ └─── user.routes.js
│ ├── node_modules
│ └─── package.json.
│ └─── .env
│ └─── server.js
User Table
user_id | user_email | user_name | user_password |
---|---|---|---|
1 | 1 | 1 | 1 |
2 | 2 | 2 | 2 |
DB Config
config/mysql.js
const mysql = require("mysql");
const connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB,
});
connection.connect(function (err) {
if (err) throw err;
console.log("DB Connected!");
});
module.exports = connection;
모델
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);
}
});
};
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);
});
};
라우터
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);
module.exports = router;
서버(수정)
server.js
const express = require("express");
const app = express();
const cors = require("cors");
const bodyParser = require("body-parser");
const morgan = require("morgan");
app.use(morgan("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
const port = process.env.PORT || 3001;
require("dotenv").config();
const UserRoute = require("./routes/user.routes");
app.use("/api/user", UserRoute);
app.get("/", (req, res) => {
res.json({
success: true,
});
});
app.listen(port, () => {
console.log(`server is listening at localhost:` + port);
});
Get Method 호출 결과
GET (findAll)
http://localhost:3001/api/user
[{"user_id":1,"user_email":"1","user_name":"1","user_password":"1"}]
[{"user_id":2,"user_email":"2","user_name":"2","user_password":"2"}]
GET (findById)
http://localhost:3001/api/user/2
[{"user_id":2,"user_email":"2","user_name":"2","user_password":"2"}]