先搭一个最小可运行的物流中枢。
npm init -y
npm i fastifyimport Fastify from 'fastify'
const fastify = Fastify({
logger: true
})
fastify.get('/ping', async function () {
return { pong: true }
})
try {
await fastify.listen({ port: 3000, host: '0.0.0.0' })
} catch (err) {
fastify.log.error(err)
process.exit(1)
}访问:
curl http://localhost:3000/ping返回:
{"pong":true}这段代码里已经有三件关键事:
Fastify():建一座物流中枢实例fastify.get(...):挂一条分拣线logger: true:打开追踪系统