
require 모듈로더(module loader)를 통해 http 서버를 생성하기 위해 express 패키지 모듈을 불러옴Express 서버를 생성하기 위해서 express 함수를 호출하여 app 변수에 할당/인 요청을 또 다른 함수로 처리하는 비지니스 로직req 객체에, 서버에서 보내야 하는 답변에 대한 정보는 res 객체res.send() 를 통해 원하는 데이터를 클라이언트에 보낼 수 있음
app.listen(port, callback) 와 같음

app.use(cors()) 를 작성하면, CORS 관련 복잡한 설정을 따로 하지 않고, cors 함수를 통해 처리할 수 있음app.post()형태의 메소드를 사용toLowerCase() 함수 사용node server/server-express.js or npm startnpx serve ./client -l 3000
server-express.js파일을 수정할 때마다 작동하는지 확인하기 위해 서버와 클라이언트를 전부 리붓을 했어야 했음 나는 서버 리붓을 계속 안하고 클라이언트만 리붓을 해서 트러블 슈팅이 안되었음





** read.js **
'use strict'
const { readAll, readOne } = require('../../model')
module.exports = async function (app, opts) {
app.get('/', async function (request, reply) {
const result = await readAll()
reply
.code(200)
.header('Content-type', 'application/json')
.send(result)
})
app.get('/:id', async function (request, reply) {
const result = await readOne( request.params.id )
if(result) {
reply
.code(200)
.header('Content-type', 'application/json')
.send(result)
}
else {
reply
.code(400)
.header('Content-type', 'text/plain')
.send('Not Found')
}
})
}
** create.js **
'use strict'
const { createOne, isValid } = require('../../model')
module.exports = async function (app, opts) {
app.post('/', async function (request, reply) {
if(!isValid(request.body)) {
reply
.code(400)
.header('Content-type', 'text/plain')
.send('Bad Request')
return;
}
const result = await createOne( request.body )
reply
.code(200)
.header('Content-type', 'application/json')
.send(result)
})
}
** update.js **
'use strict'
const { updateOne, isValid } = require('../../model')
module.exports = async function (app, opts) {
app.put('/:id', async function (request, reply) {
if(!isValid(request.body)) {
// TODO: 여기에 필요한 응답을 구현합니다.
reply
.code(400)
.header('Content-type', 'text/plain')
.send('Bad Request')
return;
}
const result = await updateOne(request.params.id, request.body)
if(result) {
reply
.code(200)
.header('Content-type', 'application/json')
.send(result)
}
else {
reply
.code(404)
.header('Content-type', 'text/plain')
.send('Not Found')
}
})
}
** delete.js **
'use strict'
const { deleteOne } = require('../../model')
module.exports = async function (app, opts) {
app.delete('/:id', async function (request, reply) {
const result = await deleteOne(request.params.id)
if(result) {
reply
.code(200)
.header('Content-type', 'application/json')
.send(result)
} else {
reply
.code(204)
.header('Content-type', 'text/plain')
.send('Not Found')
}
})
}
npm install 서버에 필요한 모듈 설치npm test 테스트
npm start 서버 시작오늘 과제는 API를 확인하고 입력하는 부분을 두시간정도 고민했다 그 이후에는 트러블 슈팅을 했는데 if문을 두번 사용해서 오류가 있었다
조원중 한명이 API 확인 하고 이후에 어떻게 옮기는지 한번 보여줬고 그것을 보고 어떻게 해야하는지 이해 후 코드를 작성했다
내일 과제까지 해놓은거라 내일은 한번 더 리마인드 하고 JS공부 해야겠다