全书目录

第十三篇:Fastify 完全指南 —— 高速物流中枢宇宙

第三章:先让中枢开门 —— 从一个最小服务开始

1 分钟 102 字 第 258 / 962 个阅读单元

先搭一个最小可运行的物流中枢。

bash
npm init -y
npm i fastify
js
import 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)
}

访问:

bash
curl http://localhost:3000/ping

返回:

json
{"pong":true}

这段代码里已经有三件关键事:

  • Fastify():建一座物流中枢实例
  • fastify.get(...):挂一条分拣线
  • logger: true:打开追踪系统