StatesAirline Server
๐ StatesAirline Server
๐ ํ์ต ๋ชฉํ
์ด๋ฅผ ํ์ตํ๊ธฐ ์ ์, ์๋ ๊ธฐ๋ณธ ๋ด์ฉ์ ๋จผ์ ์ดํดํ์
Express ์ฌ์ฉ
Refactor Express
๐ Refactor Express ๐Express Express ์ค์น npm install express Hello world! ์์ ๋ง๋ค๊ธฐ Express "Hello World" ์์ Hello world ์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ด ์ฑ์ ์ฌ๋ฌ๋ถ์ด ์์ฑํ ์ ์๋ ๊ฐ์ฅ ๊ฐ๋จํ Express ์ฑ์ผ ๊ฒ์ ๋๋ค.
hwantech.tistory.com
๐ฃ
์๋ฒ๋ฅผ ์ฒ์ ์คํ ์ํค๋ฉด ๋ฏธ๋ค์จ์ด ์ ํ ๋ถ๋ถ์ด ๋์
๊ทธํ '์ฃผ์/'๋ฅผ ๋ค์ด๊ฐ๊ฒ ๋๋ฉด ๋ผ์ฐํธ ๋ถ๋ถ์ด ์คํ๋๊ณ ๋ค์ ์์ฒญ์ ๊ธฐ๋ค๋ฆผ
์ฃผ์/๊ฐ ์๋ ๊ฒฝ์ฐ ๋ค๋ฅธ ์ฃผ์๋ก ๋ค์ด๊ฐ๋ฉด ๋ผ์ฐํธ ์คํ x ์๋ฌ์ฒ๋ฆฌ ๋์
1. ๋ฏธ๋ค์จ์ด ์ ํ ๋ถ๋ถ์ด ๋์
app.js
app.use๋ฅผ ํตํด flightRouter, bookRouter, airportRouter ๋ผ์ฐํ ์ ๋ง๋ค์ด์ฃผ๊ณ ,
cors, express.json() ์ ํ ํด ์ค ๊ฒ์ ์ ์ ์๋ค.
const express = require('express');
const cors = require('cors');
const app = express();
// ๋ชจ๋ ์๋ฒ๋ ์์ฒญ์ ๋ฐ์์ ์๋ ํฌํธ ๋ฒํธ๋ฅผ ํ์๋ก ํฉ๋๋ค.
// HTTP server์ ํ์ค ํฌํธ๋ ๋ณดํต 80 ๋ฒ ์ด์ง๋ง, ๋ณดํต ๋ค๋ฅธ ์๋ฒ์์ ์ฌ์ฉ์ค์ด๊ธฐ ๋๋ฌธ์ ์ ๊ทผํ ์ ์์ต๋๋ค.
// ๋ฐ๋ผ์ ์ฐ๋ฆฌ๋ ๋ณดํต ํ
์คํธ ์๋ฒ ํฌํธ๋ก 3000, 8080, 1337 ๋ฑ์ ํ์ฉํฉ๋๋ค.
// PORT๋ ์ํํธ์ ํธ์์๋ ๊ฐ์ต๋๋ค. ์๋ฒ๋ก ์์ฒญ์ ๋ฐ๊ธฐ ์ํด์๋ ๋ค์๊ณผ ๊ฐ์ด ํฌํธ ๋ฒํธ๋ฅผ ์ค์ ํฉ๋๋ค.
// (* ๋์ ๋ฐ๋ผ ๋ค๋ฅธ ํฌํธ๋ฒํธ๋ฅผ ์ด๊ณ ์ถ๋ค๋ฉด, ํ๊ฒฝ ๋ณ์๋ฅผ ํ์ฉ ํ๊ธฐ๋ ํฉ๋๋ค.)
const port = 3001;
const flightRouter = require('./router/flightRouter');
const bookRouter = require('./router/bookRouter');
const airportRouter = require('./router/airportRouter');
app.use(cors());
app.use(express.json());
app.use('/flight', flightRouter);
app.use('/book', bookRouter);
app.use('/airport', airportRouter);
app.get('/', (req, res) => {
res.status(200).send('Welcome, States Airline!');
});
app.use((req, res, next) => {
res.status(404).send('Not Found!');
});
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send({
message: 'Internal Server Error',
stacktrace: err.toString()
});
});
app.listen(port, () => {
console.log(`[RUN] StatesAirline Server... | http://localhost:${port}`);
});
module.exports = app;
2. '์ฃผ์/'๋ฅผ ๋ค์ด๊ฐ๋ฉด ๋ผ์ฐํธ ์คํ
airportRouter.js
router.js์๋ controller์์ ํ์ผ๋ค์์ ์์ฑ๋ ํจ์๋ฅผ ๊ฐ์ ธ์์ ์คํํ๋๋ก ๋ง๋ค์ด ์ฃผ์๋ค.
const { findAll } = require('../controller/airportController');
const express = require('express');
const router = express.Router();
router.get('/', findAll);
module.exports = router;
3. controller ๊ธฐ๋ฅ ๊ตฌํ
controller.js -> route.js -> app.js ๋ถ๋ ค์ ์ ์ฌ์ฉ
controller ๊ธฐ๋ฅ ๊ตฌํํ์ฌ ์๋ฌ์ธ์ง ์๋์ง ํ๋ณํ์ฌ ์๋ฒ์ ํํํ๋ฉด ๋๋ค.
flightController.js
const flights = require('../repository/flightList');
const fs = require('fs');
module.exports = {
// [GET] /flight
// ์์ฒญ ๋ departure_times, arrival_times, destination, departure ๊ฐ๊ณผ ๋์ผํ ๊ฐ์ ๊ฐ์ง ํญ๊ณตํธ ๋ฐ์ดํฐ๋ฅผ ์กฐํํฉ๋๋ค.
// ์์ฒญ ๋ ํ๋ผ๋ฏธํฐ departure, destination ๊ฐ๊ณผ ๋์ผํ ๊ฐ์ ๊ฐ์ง ํญ๊ณตํธ ๋ฐ์ดํฐ๋ฅผ ์กฐํํฉ๋๋ค.
findAll: (req, res) => {
const { departure_times, arrival_times, destination, departure } =
req.query;
// TODO:
if (Object.keys(req.query).length === 0) return res.json(flights);
else if (departure_times !== undefined && arrival_times !== undefined) {
const data = flights.filter(
flight =>
flight.departure_times === departure_times &&
flight.arrival_times === arrival_times
);
return res.json(data);
} else if (departure !== undefined && destination !== undefined) {
const data = flights.filter(
flight =>
flight.departure === departure && flight.destination === destination
);
return res.json(data);
} else {
return res.json('Incorrect request');
}
},
// [GET] /flight/:uuid
// ์์ฒญ ๋ uuid ๊ฐ๊ณผ ๋์ผํ uuid ๊ฐ์ ๊ฐ์ง ํญ๊ณตํธ ๋ฐ์ดํฐ๋ฅผ ์กฐํํฉ๋๋ค.
findById: (req, res) => {
// TODO:
const { uuid } = req.params;
const data = flights.filter(flight => flight.uuid === uuid);
return res.json(data);
},
// [PUT] /flight/:uuid ์์ฒญ์ ์ํํฉ๋๋ค.
// ์์ฒญ ๋ id ๊ฐ๊ณผ ๋์ผํ uuid ๊ฐ์ ๊ฐ์ง ํญ๊ณตํธ ๋ฐ์ดํฐ๋ฅผ ์์ณฅ ๋ Body ๋ฐ์ดํฐ๋ก ์์ ํฉ๋๋ค.
update: (req, res) => {
const { uuid } = req.params;
const bodyData = req.body;
//TODO:
const beUpdatedIdx = flights.findIndex(flight => flight.uuid === uuid);
const updatedFlight = { ...flights[beUpdatedIdx], ...bodyData }; //์ต์ข
์ ์ผ๋ก ์
๋ฐ์ดํธ๋ flight
flights.splice(beUpdatedIdx, 1, updatedFlight);
/* ํ์ผ ์์ */
// const jsonData = JSON.stringify(flights);
// fs.writeFileSync(
// `${__dirname}/../repository/flightList.js`,
// `module.exports = ${jsonData}`
// );
return res.status(200).json(updatedFlight);
},
};
bookController.js
const { v4: uuid } = require('uuid');
// ํญ๊ณตํธ ์์ฝ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํฉ๋๋ค.
let booking = [];
module.exports = {
// [GET] /book ์์ฒญ์ ์ํํฉ๋๋ค.
// ์ ์ฒด ์์ฝ ๋ฐ์ดํฐ๋ฅผ ์กฐํํฉ๋๋ค.
findAll: (req, res) => {
return res.status(200).json(booking);
}
,
// [GET] /book/:phone ์์ฒญ์ ์ํํฉ๋๋ค.
// ์์ฒญ ๋ phone๊ณผ ๋์ผํ phone ์์ฝ ๋ฐ์ดํฐ๋ฅผ ์กฐํํฉ๋๋ค.
findByPhone: (req, res) => {
const {phone} = req.params;
const data = booking.filter(book => book.phone === phone);
return res.status(200).json(data);
},
// [GET] /book/:phone/:flight_uuid ์์ฒญ์ ์ํํฉ๋๋ค.
// ์์ฒญ ๋ id, phone๊ณผ ๋์ผํ uuid, phone ์์ฝ ๋ฐ์ดํฐ๋ฅผ ์กฐํํฉ๋๋ค.
findByPhoneAndFlightId: (req,res) => {
const {phone, flight_uuid} = req.params;
const data = booking.filter(book => book.phone === phone && book.flight_uuid === flight_uuid);
return res.status(200).json(data);
},
// [POST] /book ์์ฒญ์ ์ํํฉ๋๋ค.
// ์์ฒญ ๋ ์์ฝ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํฉ๋๋ค.
create: (req, res) => {
const booking_uuid = uuid();
const { flight_uuid, name, phone } = req.body;
if(booking.find(book => book.phone === phone && book.flight_uuid === flight_uuid )){
return res.status(409).json("It's Already booked.")
}
else{
booking.unshift({booking_uuid, flight_uuid, name, phone});
res.location(`/book/${booking_uuid}`);
return res.status(201).json(booking[0])
}
},
// Optional
// [DELETE] /book/:booking_uuid ์์ฒญ์ ์ํํฉ๋๋ค.
// ์์ฒญ ๋ id, phone ๊ฐ๊ณผ ๋์ผํ ์์ฝ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํฉ๋๋ค.
deleteByBookingId: (req, res) => {
const {booking_uuid} = req.params;
booking = booking.filter(book => book.booking_uuid !== booking_uuid);
return res.status(200).json({booking_uuid});
}
};