mirror of
https://github.com/kou029w/_.git
synced 2025-01-30 13:58:08 +00:00
create vercel-fastify
This commit is contained in:
parent
c306eb6883
commit
2d9879ef95
11 changed files with 1919 additions and 0 deletions
2
vercel-fastify/.gitignore
vendored
Normal file
2
vercel-fastify/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
.vercel
|
||||
dist
|
19
vercel-fastify/README.md
Normal file
19
vercel-fastify/README.md
Normal file
|
@ -0,0 +1,19 @@
|
|||
## 何をしたかったか
|
||||
|
||||
Fastify と fastify-autoload を使って Vercel にデプロイできるかどうかチェック。
|
||||
|
||||
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fkou029w%2F_%2Ftree%2Fmaster%2Fvercel-fastify)
|
||||
|
||||
## 結果
|
||||
|
||||
cjs かつ includeFiles に含めればとりあえず OK。
|
||||
全部含めるなら `**` でよい。
|
||||
|
||||
```json
|
||||
{
|
||||
"functions": { "api/index.js": { "includeFiles": "**" } }
|
||||
}
|
||||
```
|
||||
|
||||
TypeScript の場合は、`vercel-build` にビルドコマンドを指定する & ビルド後に生成されるファイルを指定する。(vercel.json を参照)
|
||||
そういった設定をせず Vercel に任せると `vercel dev` では問題ないが、デプロイすると参照に失敗して 500 エラー。
|
10
vercel-fastify/api/index.ts
Normal file
10
vercel-fastify/api/index.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
import { create } from "../dist/server";
|
||||
|
||||
const server = create({ isDev: process.env.NODE_ENV === "development" });
|
||||
|
||||
async function index(req, res) {
|
||||
await server.ready();
|
||||
server.server.emit("request", req, res);
|
||||
}
|
||||
|
||||
export default index;
|
20
vercel-fastify/package.json
Normal file
20
vercel-fastify/package.json
Normal file
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"name": "@kou029w/vercel-fastify",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"build": ":",
|
||||
"vercel-build": "tsup src --clean",
|
||||
"preview": "vercel dev",
|
||||
"deploy": "vercel deploy"
|
||||
},
|
||||
"dependencies": {
|
||||
"fastify": "^3.28.0",
|
||||
"fastify-autoload": "^3.12.0",
|
||||
"pino-pretty": "^7.6.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tsup": "^5.12.6",
|
||||
"vercel": "^24.1.0"
|
||||
}
|
||||
}
|
1
vercel-fastify/public/index.txt
Normal file
1
vercel-fastify/public/index.txt
Normal file
|
@ -0,0 +1 @@
|
|||
OK
|
8
vercel-fastify/src/main.ts
Normal file
8
vercel-fastify/src/main.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
import { create, start } from "./server";
|
||||
|
||||
async function main() {
|
||||
const server = create({ isDev: true });
|
||||
await start(server, "3000");
|
||||
}
|
||||
|
||||
main();
|
7
vercel-fastify/src/routes/api/hello/index.ts
Normal file
7
vercel-fastify/src/routes/api/hello/index.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { FastifyInstance } from "fastify";
|
||||
|
||||
async function index(fastify: FastifyInstance): Promise<void> {
|
||||
fastify.get("/", async () => "Hello, World!");
|
||||
}
|
||||
|
||||
export default index;
|
9
vercel-fastify/src/routes/api/index.ts
Normal file
9
vercel-fastify/src/routes/api/index.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { FastifyInstance } from "fastify";
|
||||
|
||||
async function index(fastify: FastifyInstance): Promise<void> {
|
||||
fastify.get("/", async () => fastify.printRoutes());
|
||||
fastify.get("/query", async ({ query }) => query);
|
||||
fastify.post("/body", async ({ body }) => body);
|
||||
}
|
||||
|
||||
export default index;
|
27
vercel-fastify/src/server.ts
Normal file
27
vercel-fastify/src/server.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import fastify, { FastifyInstance } from "fastify";
|
||||
import autoload from "fastify-autoload";
|
||||
import path from "node:path";
|
||||
|
||||
type Options = {
|
||||
isDev?: boolean;
|
||||
quiet?: boolean;
|
||||
};
|
||||
|
||||
type Server = FastifyInstance;
|
||||
|
||||
export function create(options: Options): Server {
|
||||
const app = fastify({
|
||||
logger: !options.quiet && { prettyPrint: options.isDev },
|
||||
});
|
||||
app.register(autoload, {
|
||||
dir: path.resolve(__dirname, "routes"),
|
||||
routeParams: true,
|
||||
});
|
||||
return app;
|
||||
}
|
||||
|
||||
export async function start(server: Server, port: string): Promise<string> {
|
||||
await server.ready();
|
||||
const address: string = await server.listen(port, "::");
|
||||
return address;
|
||||
}
|
4
vercel-fastify/vercel.json
Normal file
4
vercel-fastify/vercel.json
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"functions": { "api/index.ts": { "includeFiles": "dist/**" } },
|
||||
"rewrites": [{ "source": "/api/(.*)", "destination": "/api/index.ts" }]
|
||||
}
|
1812
vercel-fastify/yarn.lock
Normal file
1812
vercel-fastify/yarn.lock
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue