-
사전 설치
- nodejs (가능하면 LTS)
node --version
- yarn
yarn --version
- nodejs (가능하면 LTS)
-
project 초기화
- 아래 명령을 수행하면 기본적인 정보로 package.json이 생성됩니다.
yarn init -y
- 기본 패키지 추가
yarn add fastify @fastify/swagger underscore dayjs
- 기본 개발 패키지 추가 (TEST)
yarn add tap pino-pretty nodemon -D
- 명령 실행후 dependency
"dependencies": { "@fastify/swagger": "^6.1.0", "dayjs": "^1.11.2", "fastify": "^3.29.0", "underscore": "^1.13.4" }, "devDependencies": { "nodemon": "^2.0.16", "pino-pretty": "^8.0.0", "tap": "^16.2.0" }
- 아래 명령을 수행하면 기본적인 정보로 package.json이 생성됩니다.
-
앱 작성
- 작업 디렉토리 생성
mkdir src && mkdir test
- src/app.js를 생성한다.
const fastify = require('fastify'); const build = (opts={}) => { const app = fastify(opts); app.register(require('@fastify/swagger'), {}); app.get('/hello/:name', async (req, res) => { let name = req?.params?.name; res.send({ msg: `Hello,${name}`}); }); return app; }; module.exports = build;
- src/server.js를 생성한다.
const app = require('./app'); const fastify = app({ logger: { level: 'info', prettyPrint: true } }); const start = async() => { try { const port = process.env.PORT || 3300; await fastify.listen(port, '0.0.0.0'); fastify.swagger?.(); fastify.log.info(`Server Start on ${fastify.server.address().port}`); }catch(e){ fastify.log.error(e); process.exit(1); } }; start();
- package.json에서 start script를 수정한다.
"scripts": { "test": "tap --reporter=list --watch", "dev": "nodemon --watch src/ src/server.js" },
- server를 구동한다.
yarn dev
- 127.0.0.1:3300/hello/world 로 접속해본다.
- 작업 디렉토리 생성
-
테스트 작성
- test/app.test.js 작성
const t = require('tap'); const build = require('../src/app'); t.test('/ test', async t => { const app = build(); const response = await app.inject({ method: 'GET', url: '/hello/test' }); t.equal(response.statusCode, 200); const body = JSON.parse(response.body); t.same(body.msg, 'Hello,test'); });
- test 실행
yarn test
- test/app.test.js 작성
-
문서 작성 (swagger)
- src/app.js에 swagger 기본 설정 추가
app.register(require('@fastify/swagger'), { routePrefix: '/documentation', swagger: { info: { title: 'Fastify Hands On' }, host: 'localhost:3300', schemes: ['http'], consumes: ['application/json'], produces: ['application/json'] }, exposeRoute: true });
- 적용 여부 확인
open localhost:3300/documentation
- schema 적용 (src/app.js 수정)
app.get('/hello/:name', { schema: { description: 'Hellow Test', params: { type: 'object', properties: { name: { type: 'string', description : 'user name' } } } } }, async (req, res) => {
- 재확인
-
response 추가해 보기
- src/app.js schema 수정
response: { '2xx': { type: 'object', properties: { msg: {type: 'string'} } } } ... res.send({ msg: `Hello,${name}`, info:'test version'});
-
parameter 제한해 보기
- src/app.js enum 추가
name: { type: 'string', enum: ['world', 'siva6'], description : 'user name' }
- curl로 테스트 해보기
curl -X 'GET' \ 'http://localhost:3300/hello/xworld' \ -H 'accept: application/json' {"statusCode":400,"error":"Bad Request","message":"params.name should be equal to one of the allowed values"}
-
test case 수정으로 통과율 변경
-
docker image 생성
- dockerfile 생성
FROM node:16 WORKDIR /app COPY package.json . COPY yarn.lock . COPY src/ ./src/ RUN yarn install --frozen-lockfile RUN yarn global add pm2 RUN pm2 install pm2-logrotate ENV NODE_ENV=development ENV PORT=8080 CMD ["pm2-runtime", "start", "./src/server.js", "-i", "-1"]
- docker build
docker build -t fastify-hands-on .
- docker run
docker run --name fastify-hands-on --rm -d -p 3333:8080 fastify-hands-on
- open
- dockerfile 생성
-
Notifications
You must be signed in to change notification settings - Fork 0
hohoonlee/fastify-hands-on
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
fastify 따라하기 용도
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published