#Fastify #TypeScript
https://www.fastify.io/docs/latest/TypeScript/
## インストール
```bash
npm init -y
npm i fastify
npm i -D typescript @types/node
```
## ビルド設定
`package.json`の`scripts`に設定する。
```json
{
"scripts": {
"build": "tsc -p tsconfig.json",
"start": "node index.js"
}
}
```
`tsconfig.json`の作成。
```bash
npx tsc --init
```
## ソースコード
`index.ts`の作成。
```ts
import fastify from 'fastify'
const server = fastify()
server.get('/ping', async (request, reply) => {
return 'pong\n'
})
server.listen(8080, (err, address) => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(`Server listening at ${address}`)
})
```
## 動作確認
ビルド&起動。
```bash
npm run build && npm run start
```
`/ping`が返却されればOK。
```bash
$ curl localhost:8080/ping
pong
```
## MOC
- [[FastifyでTypeScriptを使ってクエリパラメータやヘッダの型を定義]]
- [[fastifyでCORS]]