오늘의 학습내용
서버 구축하기
1. 코드 리뷰
1-1) Express 설정
const express = require('express');
const app = express();
const cors = require('cors');
const morgan = require('morgan');
app.use(morgan('tiny'));
app.use(cors());
app.use(express.json());
const port = 4000;
const discussionsRouter = require('./router/discussions');
app.use('/discussions', discussionsRouter);
app.get('/', (req, res) => {
res.status(200).send('fe-sprint-my-agora-states-server');
});
const server = app.listen(port, () => {
console.log(`[RUN] My Agora States Server... | http://localhost:${port}`);
});
module.exports.app = app;
module.exports.server = server;
1-2) ID로 필터링하기
const { discussionsController } = require('../controller');
const { findAll, findById } = discussionsController;
const express = require('express');
const router = express.Router();
router.get('/', findAll);
router.get('/:id',findById);
module.exports = router;
const { agoraStatesDiscussions } = require("../repository/discussions");
const discussionsData = agoraStatesDiscussions;
const discussionsController = {
findById: (req, res) => {
const { id } = req.params;
let filtered_id;
if(id){
filtered_id = discussionsData.filter(data => {
return data.id === Number(id);
})
}
let filter_id = filtered_id[0];
if(filter_id){
return res.status(200).json(filter_id);
} else {
return res.status(404).json('Not Found');
}
}
};
module.exports = {
discussionsController,
};