Notice
Recent Posts
Recent Comments
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
관리 메뉴

SYDev

[Express] 13. Routing 본문

대딩코딩/웹개발 스터디

[Express] 13. Routing

시데브 2023. 12. 1. 18:58

라우터를 이용해 app. 명령어 간소화

 파일명: main.js

const express = require('express');
const app = express();
const port = 3000;

const bodyParser = require('body-parser'); 
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

app.set('view engine', 'ejs');  
app.set('views', 'views');  


const expressLayouts = require('express-ejs-layouts');
app.use(expressLayouts);
app.set('layout', 'index.ejs');
app.set('layout extractStyles', true); 
app.use(express.static('public'));  

const homeController = require('./controllers/homeController.js');

app.get('/', homeController.getTopics);

//Routers
const topicRouter = require('./routers/topicRouter.js');

app.use('/topic', topicRouter); //'/topic' 형태로 받는 건 전부 topicRouter가 처리

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`);
});

 

 

 파일명: topicRouter.js

const express = require('express');
const router = express.Router();
const topicController = require('../controllers/topicController.js');

router.get('/read/:topic_id', topicController.viewTopic);    
router.get('/create', topicController.createTopic);  
router.post('/create', topicController.createNewTopic); 
router.post('/delete/:topic_id', topicController.deleteTopic);   
router.get('/update/:topic_id', topicController.updateTopic);  
router.post('/update/:topic_id', topicController.updateNewTopic);  

module.exports = router;

 

>> /topic 형태로 받는 건 전부 router가 처리, main.js가 훨씬 가벼워짐

'대딩코딩 > 웹개발 스터디' 카테고리의 다른 글

[Express] 15. Delete Comment POST  (0) 2023.12.01
[Express] 14. Create Comment POST  (0) 2023.12.01
[Express] 12. Update Topic POST  (0) 2023.12.01
[Express] 11. Delete Topic POST  (0) 2023.12.01
[Express] 10. Create Topic POST  (0) 2023.12.01