Fastify 서버 생성

소개



Fastify은 ExpressJS와 같은 웹 서버 프레임워크이지만 성능이 더 좋습니다.



생태계는 꽤 멋지다, 그adds multiple plugins . 하지만 이 첫 번째 테스트에서는 .html 파일을 가져오기 위해 fastify-static만 추가합니다.

코딩하자!



처음에는 void 폴더를 만들고 Fastify 및 fastify-static을 설치합니다.

npm i fastify fastify-static


app.js를 생성합니다. 루트 파일입니다.

app.js로



새 Fastify 서버를 생성하기 위해 이 파일의 기초를 작성할 수 있습니다.

const path = require("path")
const f = require('fastify')({logger: false})

f.register(require('fastify-static'), {
    root: path.join(__dirname, 'public'),
    prefix: '/public/',
})

// In this example, when you get localhost:3000, ou have the time
f.get('/', (request, reply) => {
    reply.header('Content-Type', 'application/json')
    reply.send({hello: new Date()})
})
f.get('/about', (request, reply) => {
    reply.sendFile('about.html' )
})


const start = async () => {
    try {
        await f.listen(3000)
    } catch (err) {
        f.log.error(err)
        process.exit(1)
    }
}
start().then(r => r)


공개 HTML 페이지



/public 폴더와 about.html 파일을 만듭니다.





매우 짧은 게시물이지만 Fastify로 간단하게 서버를 시작하는 방법을 시연합니다. 처음 사용하는거라 약간의 오차가 있을 수 있습니다. 댓글로 피드백을 남겨주세요! 👍🏼

Getting Started with Fastify




내 . 많은 프로젝트와 업데이트를 볼 수 있습니다. 할 수도 있습니다support me on Buy Me a Coffee.

좋은 웹페이지 즐겨찾기