JSON파일로 간단하게 데이터베이스를 만들어보자.
서버에 다시접속해도 상관없이 일정한 데이터에 접근할수있어서 장점임.
참고로 데이터베이스가 지원해주는 안정장치가 없어 불안정한 데이터이기때문에 토이프로젝트용임.
준비
database.json
{
"posts": [
{
"id": "1",
"title": "my first post",
"content": "hi"
},
{
"id": "2",
"title": "node basic",
"content": "database with json"
}
]
}
database.back.json
데이터베이스 json파일을 읽어오려면 노드js에서 제공하는 fs(file system) 모듈이 필요하다.
기존의 posts 는 사라지고 getPosts(), savePosts()가 생기면서 관련 코드도 변경된다.
api.js
//@ts-check
/**
* @typedef Post
* @property {string} id
* @property {string} title
* @property {string} content
*/
// /** @type {Post[]} */
// const posts = [
// { id: '1', title: 'my first post', content: 'hi' },
// { id: '2', title: 'my second post', content: 'bye' },
// ]
/**
* @typedef APIResponse
* @property {number} statusCode
* @property {string | object} body
*/
// /**
// * @tyoedef Route
// * @property {string} url
// * @property {string} method
// * @property {(valuse:Object)=> string} callback
// */
/**
* @typedef Route
* @property {RegExp} url
* @property {'GET' | 'POST'} method
* @property {(maches: string[], body: object | undefined)=> Promise<APIResponse>} callback
*/
const fs = require('fs')
const DB_JSON_FILENAME = 'database.json' //db파일위치 절대경로
/** @returns {Promise<Post[]>} */
async function getPosts() {
const json = await fs.promises.readFile(DB_JSON_FILENAME, 'utf-8')
return JSON.parse(json).posts
}
/** @param {Post[]} posts */
async function savePosts(posts) {
const content = {
posts,
}
await fs.promises.writeFile(
DB_JSON_FILENAME,
JSON.stringify(content),
'utf-8'
)
}
/** @type {Route[]} */
const routes = [
{
url: /^\/posts$/, //'posts' -> regExe로 고쳐야함
method: 'GET',
callback: async () => ({
statusCode: 200,
// body: posts,
body: await getPosts(),
}),
},
{
url: /^\/posts\/([a-zA-Z0-9-_]+)$/, //'posts/:id' -> regExe
method: 'GET',
callback: async (maches) => {
console.log(maches)
const postId = maches[1]
if (!postId) {
return {
statusCode: 404,
body: 'Not found3',
}
}
// const post = posts.find((_post) => _post.id === postId)
const posts = await getPosts()
const post = posts.find((_post) => _post.id === postId)
if (!post) {
return {
statusCode: 404,
body: 'Not found',
}
}
return {
statusCode: 200,
body: post,
}
},
},
{
url: /^\/posts$/, //'posts' -> regExe
method: 'POST',
callback: async (_, body) => {
if (!body) {
return {
statusCode: 400,
body: 'no body',
}
}
/** @type {string} */
const title = body.title
const newPost = {
id: title.replace(/\s/g, '_'),
title,
content: body.content,
}
const posts = await getPosts()
posts.push(newPost)
// 업데이트된 포스트를 저장
savePosts(posts)
return {
statusCode: 200,
body: newPost,
}
},
},
]
// 이 파일은 모듈이고 모듈에서 route를 내보냄
module.exports = {
routes,
}
main.js 내용은 이전포스팅참고
반응형
'Backend > node.js' 카테고리의 다른 글
[express] 미들웨어(+ 에러핸들링) (0) | 2021.09.07 |
---|---|
[node.js] express 설치와 환경세팅 (0) | 2021.09.04 |
[nodejs] 간단한 restfulAPI 서버만들기3 - 코드개선(추상화), 리펙토링 (0) | 2021.09.01 |
[nodejs] 간단한 restfulAPI 서버만들기2 (0) | 2021.08.30 |
[nodejs] 간단한 restfulAPI 서버만들기 (0) | 2021.08.27 |