| 데이터 모킹
app.ts에 이런식으로 작성하고
import * as express from "express";
const app: express.Express = express();
const data = [1, 2, 3, 4];
app.get("/", (req, res) => {
console.log(req);
res.send({ data });
});
app.listen(8000, () => {
console.log("server is on...");
});
post man으로 요청을 해보자
위와 같이 데이터가 잘나온다
그런데 사실 1,2,3,4 와 같은 데이터는 의미가 없다
그래서 의미있는데이터들을 한번 써볼건데
src에서 app.model.ts 라는 파일을 만들어준다
그리고 app.model.ts 에 아래와같이 테스트용으로 좀 의미가 있는 데이터를 만드는것을 데이터모킹이라고 한다
모의 데이터인 셈이다
아래는 고양이 데이터 예시이다
export type CatType = {
id: string;
name: string;
age: number;
species: string;
isCute: boolean;
friends: string[];
};
export const Cat: CatType[] = [
{
id: 'fsduifh',
name: 'blue',
age: 8,
species: 'Russian Blue',
isCute: true,
friends: ['asdfhj29009', 'WE09tju2j'],
},
{
id: 'iohf2309q4hr',
name: 'som',
age: 4,
species: 'Sphynx cat',
isCute: true,
friends: ['weju0fj20qj', 'asdfhj29009', 'weju0fj20qj'],
},
{
id: 'WE09tju2j',
name: 'lean',
age: 6,
species: 'Munchkin',
isCute: false,
friends: [],
},
{
id: 'asdfhj29009',
name: 'star',
age: 10,
species: 'Scottish Fold',
isCute: true,
friends: ['weju0fj20qj'],
},
{
id: 'weju0fj20qj',
name: 'red',
age: 2,
species: 'Sharm',
isCute: false,
friends: [],
},
];
이걸 그대로 app.ts 에 가져오도록 하자
아래와같이 상대경로로 불러온다음 다시 서버를 열고
import * as express from "express";
import { Cat, CatType } from "./app.model";
const app: express.Express = express();
const data = [1, 2, 3, 4];
app.get("/", (req, res) => {
console.log(req);
res.send({ cats: Cat });
});
app.listen(8000, () => {
console.log("server is on...");
});
postman으로 요청을해보자
고양이정보가 잘 넘어온것을 볼 수 있다
반응형
'웹 개발 > 🟩 Node.js' 카테고리의 다른 글
Express | route분리, 모듈화 (0) | 2023.06.18 |
---|---|
Express | Create Read API 개발 (0) | 2023.06.18 |
Express | express에서 미들웨어설정하기 (0) | 2023.06.13 |
Express | express, ts 개발 환경 셋업, helloworld출력하기 (0) | 2023.06.12 |
Express | Node, VSCode, Prettier 개발환경세팅 (0) | 2023.06.05 |