/MARU-SERVER_API

πŸ“šBOOK-MARUπŸ“š

Primary LanguageJavaScript

MARU SERVER

뉴노멀 μ‹œλŒ€μ— μ–Έμ œ, μ–΄λ””μ„œλ‚˜ 인문학적 μ†Œμ–‘μ„ κΈ°λ₯Ό 수 있고 μ§„μ†”ν•œ μ†Œν†΅μ„ ν•  수 μžˆλŠ” 곡간, λ§ˆλ£¨μž…λ‹ˆλ‹€.

MARUλŠ” λŒ€ν•™μƒ μ—°ν•© IT λ²€μ²˜μ°½μ—… 동아리 SOPT λ‚΄μ—μ„œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜ 개발 ν”„λ‘œμ νŠΈλ₯Ό ν•¨κ»˜ν•œ λ©€λ²„λ“€λ‘œ κ΅¬μ„±λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

νŒ€ λ‹¨μœ„μ˜ ν˜‘μ—…μ„ 같이 κ²½ν—˜ν•˜κ³  λ‹€μ–‘ν•œ 관점듀을 κ³΅μœ ν•œ μ €ν¬λŠ” β€œλ§ˆλ£¨β€λ₯Ό κ°œλ°œν•˜λ©΄μ„œ λ‹€μ‹œ ν•œ 번 νŒ€ λ‹¨μœ„μ˜ μ—­λŸ‰μ„ λ°°μ–‘ν•  수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.



😊 About MARU

API Specification

Project Period : 2020.08~2020.09

Member : μ •νš¨μ›, μ΅œμ •κ· , μ†μ˜ˆμ§€


πŸ’₯ Main Function

μ†ŒμΌ“ν†΅μ‹ μ„ μ΄μš©ν•œ ν† λ‘  - μ •νš¨μ›

// 'connection' 이벀트 λ°œμƒ
io.on('connection', (socket) => {

  // ν΄λΌμ΄μ–ΈνŠΈ 접속 λŠμ„ μ‹œ on('disconnect') λ°œμƒ
  socket.on('disconnect', () => {
    console.log('user disconnected');
  });
  // room λ‚˜κ°€κΈ° : socket.leave
  socket.on('leaveRoom', (roomIdx, name) => {
    socket.leave(room[roomIdx], () => {
      io.to(room[roomIdx]).emit('leaveRoom', roomIdx, name);
    });
  });
  // ν† λ‘ μ—μ„œ λ²—μ–΄λ‚œ μ‹œκ°„ 체크
  socket.on('leave', (name, roomIdx) => {
    var date=new Date();
    let disconnectTime = moment(date).format('YYYY-MM-DD HH:mm:ss');

    //λ””λΉ„ 톡신 λΆ€λΆ„
  });

  // νŠΉμ • room μ—κ²Œ 이벀트 보낼 μ‹œ : io.to('room이름').emit()
    socket.on('joinRoom', (roomIdx, name) => {
    socket.join(room[roomIdx], () => {
      room.push('room'+roomIdx);
      io.to(room[roomIdx]).emit('joinRoom', roomIdx,name);
    });
  });

//클라 : socket.emit('chat message') , μ„œλ²„ : socket.on('chat message') λ§€κ°œλ³€μˆ˜ : name, msg, roomIdx
  socket.on('chat message', (name, msg, roomIdx) => {
    a=roomIdx;
    var date = new Date(); 
    let chatTime = moment(date).format('YYYY-MM-DD HH:mm:ss');

    //λ””λΉ„ 톡신 λΆ€λΆ„
    io.to(room[a]).emit('chat message', name, msg);
  });
  });
});

KAKAO APIλ₯Ό μ΄μš©ν•œ μ±… 검색 - μ΅œμ •κ· 

const options = {
 kakaoTest: async(title) => {
   return {
     url: `https://dapi.kakao.com/v3/search/book?target=title`,
     method: 'GET',
     headers: {
       'Authorization': '',
     },
     qs: {
       query : title,
     },
     encoding: 'UTF-8',
   }
 }
}
module.exports = options;
request(kakao, function (err, response, body) {
if (!err && response.statusCode == 200) {
 const book = JSON.parse(body).documents;
 
 // κ²€μƒ‰ν•œ κ²°κ³Όκ°€ μ‘΄μž¬ν•˜μ§€ μ•Šμ„ λ•Œ
 if (book.length === 0) {
   res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, resMessage.NO_SEARCH));
   return;
 }

 // 검색 κ²°κ³Όκ°€ μ‘΄μž¬ν•  λ•Œ
 bookList = [];
 
 for (let item of book) {
   bookList.push({
     authors : item.authors[0],
     title : item.title,
     thumbnail : item.thumbnail
   })
 }

 res.status(statusCode.OK).send(util.success(statusCode.OK, resMessage.SUCCESS_SEARCH, bookList))
}

Firebaseλ₯Ό μ΄μš©ν•œ μ•Œλ¦Ό κ΅¬ν˜„ - μ†μ˜ˆμ§€

try {
    const deviceTokens = await alarmModel.getDeviceToken(roomIdx, nickName);
    const registrationTokens = []; //λ””λ°”μ΄μŠ€ 등둝

    for (let i = 0; i < deviceTokens.length; ++i) {
        if (deviceTokens[i].deviceToken == null) {
            res.status(statusCode.DB_ERROR).send(util.fail(statusCode.DB_ERROR, resMessage.INTERNAL_SERVER_ERROR));
            return;
        }
        registrationTokens.push(deviceTokens[i].deviceToken);
    }

    if (!admin.apps.length) {
        admin.initializeApp({
            credential: admin.credential.cert(firebaseConfig),
            databaseURL: ""
        });
    }
    var options = {
        priority: 'high',
        timeToLive: 60 * 60 * 24 * 2
    };

    //payload
    var payload = {
        notification: {
            title: title,
            body: nickName + " : " + message,
        },
    };

πŸ’» package.json

"dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "dotenv": "^8.2.0",
    "ejs": "^3.1.5",
    "express": "~4.16.1",
    "fcm-node": "^1.5.2",
    "firebase-admin": "^9.2.0",
    "http-errors": "^1.6.3",
    "jsonwebtoken": "^8.5.1",
    "moment": "^2.27.0",
    "moment-timezone": "^0.5.31",
    "morgan": "~1.9.1",
    "nodemon": "^2.0.4",
    "pbkdf2": "^3.1.1",
    "promise-mysql": "^4.1.3",
    "rand-token": "^1.0.1",
    "request": "^2.88.2",
    "serve-favicon": "^2.5.0",
    "socket.io": "^2.3.0"
  }

🌟 RULE

  • Coding Convention
  • Git Convention

πŸ’¬ Code Convention

❗naming

1. **λ³€μˆ˜λͺ…, 파일λͺ…** : μΉ΄λ©œμΌ€μ΄μŠ€  (`inputBox`)
2. **폴더λͺ…** : μ†Œλ¬Έμž (`inputbox`)
3. **클래슀λͺ…** : μ²«κΈ€μž λŒ€λ¬Έμž (`InputBox`)
  • var λ³΄λ‹€λŠ” let, const 지ν–₯
  • 비동기
    • promise의 thenλ³΄λ‹€λŠ” async/await μ‚¬μš©

πŸ‘‰ Git Convention

branch

  • master
    • develop
    • feature_jg
    • feature_yz
    • feature_hw

Commit Message

CREATE - κΈ°λŠ₯ κ΅¬ν˜„
UPDATE - μ½”λ“œ μˆ˜μ •
MERGE - μ½”λ“œ 병합
FIX - 버그 μˆ˜μ •
RELEASE - 버전 배포
DELETE - κΈ°λŠ₯ μ‚­μ œ
DOCS - λ¬Έμ„œ νŽΈμ§‘