https://www.youtube.com/watch?v=pN49Lnlyuao
https://github.com/typicode/json-server
-> Get a full fake REST API with zero coding in less than 30 seconds (seriously) .... 시리어슬리!!!
$ sudo npm install -g json-server
[sudo] centumjoonho 암호:
added 109 packages, and audited 110 packages in 5s
10 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
db.json 파일 생성 !
{
"users": [
{
"id": 0,
"name": "John Doe",
"email": "johndoe@example.com",
"password": "password"
},
{
"id": 1,
"name": "ane Doe",
"email": "anedoe@example.com",
"password": "password"
},
{
"id": 2,
"name": "Peter Doe",
"email": "peterdoe@example.com",
"password": "password"
},
{
"id": 3,
"name": "Mary Doe",
"email": "marydoe@example.com",
"password": "password"
},
{
"id": 4,
"name": "ter Doe",
"email": "terdoe@example.com",
"password": "password"
},
{
"id": 5,
"name": "setMary Doe",
"email": "setmarydoe@example.com",
"password": "password"
},
{
"id": 6,
"name": "Peterfan Doe",
"email": "peterfandoe@example.com",
"password": "password"
},
{
"id": 7,
"name": "Marymne Doe",
"email": "marymnedoe@example.com",
"password": "password"
},
{
"id": 8,
"name": " Doeddd",
"email": "doeddd@example.com",
"password": "password"
},
{
"id": 9,
"name": "Maria Doe",
"email": "Mariadoe@example.com",
"password": "password"
}
]
}
-> 이런 더미 데이터를 넣어줬다 !
->Getting started
Getting started
Install JSON Server
npm install -g json-server
Create a db.json file with some data
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
}
Start JSON Server
json-server --watch db.json
Now if you go to http://localhost:3000/posts/1, you'll get
{ "id": 1, "title": "json-server", "author": "typicode" }
Also when doing requests, it's good to know that:
- If you make POST, PUT, PATCH or DELETE requests, changes will be automatically and safely saved to db.json using lowdb.
- Your request body JSON should be object enclosed, just like the GET output. (for example {"name": "Foobar"})
- Id values are not mutable. Any id value in the body of your PUT or PATCH request will be ignored. Only a value set in a POST request will be respected, but only if not already taken.
- A POST, PUT or PATCH request should include a Content-Type: application/json header to use the JSON in the request body. Otherwise it will return a 2XX status code, but without changes being made to the data.
-> $ json-server --watch db.json 해당 명령어 터미널 입력
$json-server --watch db.json
\{^_^}/ hi!
Loading db.json
Done
Resources
http://localhost:3000/users
Home
http://localhost:3000
Type s + enter at any time to create a snapshot of the database
Watching...
PostMan 설치 해보자 !!
POSTMAN
installation 완료 !
GET -> http://localhost:3000/users/3
->
YEP,,,, RIGHT THAT
미들웨어 ! 코드를 한번 보자
module.exports = (req, res, next) => {
res.header("X-Hello", "World");
if (req.path == "/users") {
next();
} else {
res.status(404).json({ err: "넌 못지나간다" });
}
};
명령어 입력 ->
json-server db.json --middlewares ./hello.js
주소를 조금만 바꿔도
err : "넌 못 지나간다 "
라우터 !! 코드를 한번 보자
Add custom routes
Create a routes.json file. Pay attention to start every route with /.
{
"/api/*": "/$1",
"/:resource/:id/show": "/:resource/:id",
"/posts/:category": "/posts?category=:category",
"/articles\\?id=:id": "/posts/:id"
}
Start JSON Server with --routes option.
json-server db.json --routes routes.json
Now you can access resources using additional routes.
/api/posts # → /posts
/api/posts/1 # → /posts/1
/posts/1/show # → /posts/1
/posts/javascript # → /posts?category=javascript
/articles?id=1 # → /posts/1
http://localhost:5000/api/users 해당 주소로도 접속이 가능
-> 쉽게 말해 내가 원하는 url 을 만들 수 있다.
'Web Dev > node.js' 카테고리의 다른 글
Nest.js 의 Module 모듈 이란? (1) | 2024.05.28 |
---|---|
Node.js에서 mongoose 사용하여 MongoDB 연동 하는 방법 요로코롬 (1) | 2023.10.20 |
Node.js Api -> MongoDB 전송 칼럼명 맞추지 않으면 전송 안되지롱! (2) | 2023.07.03 |
Node.js 우분투에 노드js로 기초 백엔드 api 서버 구축 코드를 알아보자 (0) | 2023.03.13 |
Node.js CoolSMS를 사용하여 백엔드 api로 문자 SMS 발송 하는 방법 알려드립니다. (2) | 2023.03.13 |
Node.js - Nodemon 사용방법 : 서버 셧다운 필요없는 자동 서버 재실행 in 우분투 (0) | 2023.03.09 |