src
│ app.js # App entry point
└───api # api
└───config # 환경 설정
└───loaders # 시작에 해야될 환경 설정 모듈 ( ex. app.use() 등 )
└───models # Database models
└───services # api에서 제공하는 서비스 로직이 있는 코드
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 ) 등의 메소드를 처리하는 파일 및 함수
로직이나 직접적인 코드를 controller에 넣지 않기
→ 많은 req, res를 받기 떄문에, 스파게티 코드가 될 가능성이 높음
위에 나와 있는 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;
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;
}
}