1
0
Fork 0
mirror of https://github.com/kou029w/_.git synced 2025-01-30 13:58:08 +00:00

create vercel-fastify

This commit is contained in:
Nebel 2022-04-26 10:51:36 +09:00
parent c306eb6883
commit 2d9879ef95
11 changed files with 1919 additions and 0 deletions

2
vercel-fastify/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.vercel
dist

19
vercel-fastify/README.md Normal file
View 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 エラー。

View 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;

View 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"
}
}

View file

@ -0,0 +1 @@
OK

View file

@ -0,0 +1,8 @@
import { create, start } from "./server";
async function main() {
const server = create({ isDev: true });
await start(server, "3000");
}
main();

View file

@ -0,0 +1,7 @@
import { FastifyInstance } from "fastify";
async function index(fastify: FastifyInstance): Promise<void> {
fastify.get("/", async () => "Hello, World!");
}
export default index;

View 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;

View 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;
}

View 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

File diff suppressed because it is too large Load diff