Nodejs: The order of web development

2023. 11. 20. 23:43Javascript Node.js

Development > Test > Web design(backend) > Development > Test > Distribution

1. API statement

https://docs.google.com/spreadsheets/d/1CdmB4TMBXHJwzYmALGMJzyT9n-SPN38s6sWaColrDoA/edit?usp=sharing

 

API 명세서 최종현

API 명세서 템플릿 Category,Description,Method,URL,Request Headers,Request Body,Response Headers,Response Body - 성공,Response Body - 실패,비고 인증,회원가입 API,POST,/auth/signup,Content-Type: application/json,# 데이터 타입 { "email

docs.google.com

 

2. Relational database modeling

 

3. ERD cloud

https://www.erdcloud.com/d/S99qksQR3hesDqYbh

 

sparta-node-intermediate

Draw ERD with your team members. All states are shared in real time. And it's FREE. Database modeling tool.

www.erdcloud.com

 

4. Coding Development

{
  "name": "nodjs_webpage",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "nodemon app.js",
    "migrate:up": "npx sequelize db:migrate --config config/config.cjs",
    "migrate:down": "npx sequelize db:migrate:undo --config config/config.cjs"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.1.1",
    "dotenv": "^16.3.1",
    "express": "^4.18.2",
    "mysql2": "^3.6.3",
    "sequelize": "^6.35.0"
  },
  "devDependencies": {
    "nodemon": "^3.0.1",
    "sequelize-cli": "^6.6.2"
  }
}

Dependencies npm install: bcrypt, dotenv, express, mysql2, sequelize

DevDependencies npm install: nodemon, sequelize-cli

// app.js

import express from 'express';
import { SERVER_PORT } from './constants/app.constant.js';
import { apiRouter } from './routers/index.js';

// express를 app으로 변수선언하여 바로 실행
const app = express();
// Express 애플리케이션에서 JSON 형태의 요청(request) body를 파싱(parse)하기 위해 사용되는 미들웨어(middleware)
app.use(express.json());
// 인코딩된 request의 payload를 파싱해주는 미들웨어
app.use(express.urlencoded({ extended: true }));
// localhost:<port number>/api
app.use('/api', apiRouter);
// Server 연결
app.listen(SERVER_PORT, () => {
  console.log(`App listening on port ${SERVER_PORT}`);
});
 

 

// ./models/index.cjs
// Sequelize ORM 구조
// sequelize는 ORM(Object-Relational Mapping)로 분류
// ORM이란 객체와 관계형 데이터베이스의 관계를 매핑 해주는 도구이다.
'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const process = require('process');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
// config 파일을 .cjs로 변경한 점이 특이점이다. 이유는 현재 프로젝트는 model type으로 진행
const config = require(__dirname + '/../config/config.cjs')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(
    config.database,
    config.username,
    config.password,
    config,
  );
}

fs.readdirSync(__dirname)
  .filter((file) => {
    return (
      file.indexOf('.') !== 0 &&
      file !== basename &&
      file.slice(-4) === '.cjs' &&
      file.indexOf('.test.js') === -1
    );
  })
  .forEach((file) => {
    const model = require(path.join(__dirname, file))(
      sequelize,
      Sequelize.DataTypes,
    );
    db[model.name] = model;
  });

Object.keys(db).forEach((modelName) => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;