폴더 구조

src
│   app.js          # App entry point
└───api             # api
└───config          # 환경 설정
└───loaders         # 시작에 해야될 환경 설정 모듈 ( ex. app.use() 등 )
└───models          # Database models
└───services        # api에서 제공하는 서비스 로직이 있는 코드

3계층 설계

Untitled

Untitled

controller : express route controller

import express from 'express';

import auth from './auth/index.js';
import img from './img/index.js';
import chat from './chat/index.js';

const router = express.Router();

router.use('/auth', auth);
router.use('/img', img);
router.use('/chat', chat);

export default router;

import express from "express";

import githubAuthService from '#services/auth/github'
import userService from '#services/auth/user-service';

const router = express.Router();

router.post('/', (req, res, next) => {
		const data = githubAuthService();
		
		res.json({data});
});

export default router;

backend에서 path 와 ( get,post ) 등의 메소드를 처리하는 파일 및 함수

service

위에 나와 있는 githubAuthService와 같이 post이후에, req,res,next를 받아서 처리해주는 로직 및 함수

import fetch from 'node-fetch';
import { AuthError } from '#services/errors/index';
import config from '#config/index';

async function github() {
    ...
getData();
		return data;
}

export default github;

Mysql ORM == Sequelize

const getData = async (username) => {
		try {
			const res = await db.User.findOne({ where: { username: username } });

			if (res === null)
				return null;
			const userInfo = res.dataValues;
			
			return userInfo;
		} catch (err) {
			throw err;
		}
	}