Today I Learned (2024.02.27)

다율·2024년 2월 27일

TIL

목록 보기
20/23
post-thumbnail

Sequelize로 API 만들기

CRUD

  • 기본적인 기능 4가지
    • C : Crete
    • R : Read
    • U : Update
    • D : Delete

API 코드 설명 (create, read)

  • Postman으로 test해보기
// mainRouter.js

const db = require('./model/db');   // DB를 사용하기 위해 exports한 것을 불러옴

// Create
router.get("/data/create", function(req,res){
    let user_id = parseInt(Math.random() * 10000);
    db.users.create({user_id:user_id}).then(function(result){
        res.send({success:200});
    })  // 객체로 값을 넘겨줌

})

// Read
router.get("/data/read", function(req,res){
    db.users.findAll().then(function(result){       // findAll값이 result에 담김
        res.send({succsess:200});
    })
})

깃허브 링크

https://github.com/dayul/web-study/tree/main/chapter_2/node-study

profile
새싹 개발자 🌱

0개의 댓글