CRUD + Auth MYSQL버전 라우트 복잡도 낮추기
이번에는 라우트의 복잡도를 낮추는 작업을 해보자. (업로드 기능은 삭제함)
topic에 관련된 라우트는 topic.js로 다 옮김
var express = require('express');//익스프레스를 가져옴
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false }));//body parser를 사용함
app.locals.pretty = true;//템플릿 줄바꿈
app.set('views', './views/mysql');//템플릿 폴더 위치 표시
app.set('view engine', 'jade');//템플릿 엔진을 jade로 명시함
app.use('/user', express.static('uploads'));//여기로 폴더에 있는 그림을 접근할 수 있음
var topic = require('./routes/mysql/topic')();
app.use('/topic', topic);// topic으로 들어오면 topic이라는 객체에 위임함
console.log('Connected, 3003 port!');
var route = require('express').Router();
var conn = require('../../config/mysql/db')();
route.get('/add', function(req, res){
var sql = 'SELECT id, title FROM topic';//글목록을 보여주기 위한 쿼리문
conn.query(sql, function(err, topics, fields){
res.status(500).send('Internal Server Error');
res.render('topic/add', {topics:topics});
route.get(['/:id/edit'], function(req, res){
var sql = 'SELECT id, title FROM topic';//글목록을 보여주기 위한 쿼리문
conn.query(sql, function(err, topics, fields){
var id = req.params.id;//:id값을 가져옴
var sql = 'SELECT * FROM topic WHERE id=?';
conn.query(sql, [id], function(err, topic, fields){
res.status(500).send('Internal Server Error');
res.render('topic/edit', {topics:topics, topic:topic[0]});//topic은 쿼리문의 데이터 한개만 넘겨줌
console.log('There is no id.');
res.staus(500).send('Internal server error');
route.post(['/:id/edit'], function(req, res){
var description = req.body.description;
var sql = 'UPDATE topic SET title=?, description=?, author=? WHERE id=?';
conn.query(sql, [title, description, author, id], function(err, result, fields){
res.status(500).send('Internal Server Error');
//res.send(result);//이걸로 먼저 result값을 보고서 해야한다.
res.redirect('/topic/'+id);//변경후 이동 시켜라
route.get('/:id/delete', function(req, res){
var sql = 'SELECT id, title FROM topic';//글목록을 보여주기 위한 쿼리문
conn.query(sql, function(err, topics, fields){
var sql = 'SELECT * FROM topic WHERE id=?';
conn.query(sql, [id], function(err, topic){
res.status(500).send('Internal Server Error');
console.log('There is no record.');
res.status(500).send('Internal Server Error');
res.render('topic/delete', {topics:topics, topic:topic[0]});
route.post('/:id/delete', function(req, res){
var sql = 'DELETE FROM topic WHERE id=?';
conn.query(sql, [id], function(err, result){
res.redirect('/topic/');//삭제되면 초기화면으로감
route.post('/add', function(req, res){
var title = req.body.title;//제목과 본문의 내용을 가져옴
var description = req.body.description;
var sql = 'INSERT INTO topic(title, description, author) VALUES(?, ?, ?)';
conn.query(sql, [title, description, author], function(err, result, fields){
res.status(500).send('Internal Server Error');
res.redirect('/topic/'+result.insertId);//insertId는 입력한 행의 id값을 의미함
//[]는 배열을 의미 둘다 여기로 접근 가능하다는 뜻
route.get(['/', '/:id'], function(req, res){
var sql = 'SELECT id, title FROM topic';//글목록을 보여주기 위한 쿼리문
conn.query(sql, function(err, topics, fields){
var id = req.params.id;//:id값을 가져옴
var sql = 'SELECT * FROM topic WHERE id=?';
conn.query(sql, [id], function(err, topic, fields){
res.status(500).send('Internal Server Error');
res.render('topic/view', {topics:topics, topic:topic[0]});//topic은 쿼리문의 데이터 한개만 넘겨줌
res.render('topic/view', {topics:topics});//view.jade파일을 출력함 쿼리문에서 가져온 값을 넘겨줌
'프로그래밍 > Node.js' 카테고리의 다른 글
Node.js 생활코딩 정리 – CRUD + Auth MYSQL 버전 모두 합치기 (0) | 2016.12.19 |
---|---|
Node.js 생활코딩 정리 – CRUD + Auth MYSQL버전을 여러 개의 파일로 나누기3 (0) | 2016.12.16 |
Node.js 생활코딩 정리 – CRUD+Auth MYSQL버전을 여러개의 파일로 나누기2 (0) | 2016.12.14 |
Node.js 생활코딩 정리 – CRUD + Auth MYSQL버전을 여러개의 파일로 나누기 1 (0) | 2016.12.13 |
Node.js 생활코딩 정리 – 사용자 정의 모듈 만들기 (0) | 2016.12.11 |
Node.js 생활코딩 정리 – jade extends 란? (0) | 2016.12.10 |
Node.js 생활코딩 정리 – Mysql을 이용한 Login 구현 (0) | 2016.12.09 |
Node.js 생활코딩 정리 – MYSQL을 이용한 회원가입(Register) (0) | 2016.12.08 |