TIL_20.06.20(토)~21(일)

nRecode·2020년 6월 21일
0

TodayILearned

목록 보기
62/95
post-thumbnail

20.06.20(토)

res.json과 res.send

res.json(obj)
obj가 JSON 문자열로 변환되어 body라는 변수에 저장된다. Content-Type 헤더가 세팅되지 않았을 경우 this(res 객체)에 Content-Type 으로 application/json을 세팅한다. 그리고 마지막으로 res.send(body)를 실행하면서 결과를 반환한다! 마지막으로 실행되는 res.send(body)의 인자 body는 string 형식이다.

  1. res.json(object)
  2. res.send(string)

res.send(body)
res.send(body)의 함수의 인자로는 body가 있는데, body는 chuck로 할당되고 chunk에 타입검사가 이루어진다. 만약 res.send(object)로 코드를 실행했을 때, 실행순서는 아래와 같다.

  1. res.send(object)
  2. res.json(object)
  3. res.send(string)

reference

20.06.21(일)

hook API에서 유저가 signup 할 때, password를 해싱한 것을 확인 할 때, afterValidate라는 것을 이용했는데, 활용할 수가 없어서 reference코드를 참고하였다.

hooks: {
 
  //비밀번호를 해싱해서 데이터베이스에 저장
  beforeCreate: (data, option) => {
    var shasum = crypto.createHmac('sha512', 'thisismysecretkey');
    shasum.update(data.password);
    data.password = shasum.digest('hex');
  },
        //데이터를 찾기 전에 들어온 데이터의 password를 해싱함수를 통해 검증할 수 있게 함
  beforeFind: (data, option) => {
    if (data.where.password) {
      var shasum = crypto.createHmac('sha512', 'thisismysecretkey');
      shasum.update(data.where.password);
      data.where.password = shasum.digest('hex');
    }
  }
}

promise를 이용하여 sequelize로 db를 생성하고 연결하는데, then((data,err)=>{})의 두번째 인자인 err와 catch(err => {})은 같음.

profile
안정성, 확장성 있는 서버를 구축하고 가꾸는 개발자를 목표로 공부하고 있습니다. 🤔🤔🤔🤔 부족하기에 맞지 않는 내용이 있을 수 있습니다. 가감없이 피드백 해주시면 정말 감사하겠습니다..🙏

0개의 댓글