ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • StatesAirline Server
    codeStates front-end/node(server) 2023. 2. 9. 15:54
    ๋ฐ˜์‘ํ˜•

     

     

     

    ๐Ÿ“Œ 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});
      }
    };
    ๋ฐ˜์‘ํ˜•

    'codeStates front-end > node(server)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    JSON.stringify  (0) 2023.02.14
    My Agora states server  (0) 2023.02.09
    Refactor Express  (0) 2023.02.08
    Mini-Node Server  (0) 2023.02.08
    CORS ๊ฐœ์š”  (0) 2023.02.06

    ๋Œ“๊ธ€

Designed by Tistory.