Skip to content

Update README.md #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 12 additions & 119 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
# Fastify SWC TypeScript Server

## Overview

This project leverages Fastify, TypeScript, and SWC to build a high-performance, scalable server. Fastify offers unmatched scalability and speed, TypeScript provides strong typing and advanced features, and SWC handles rapid transpilation. **Notably, the `typescript` package is not a dependency**—SWC transpiles TypeScript independently, providing fast, lightweight builds.

In **development mode**, the server updates instantly to reflect changes in source files or tests. In **production**, it operates on optimized JavaScript for better performance.

This server template can be an ideal foundation for developing [serverless functions](https://en.wikipedia.org/wiki/Serverless_computing), which are advantageous in modern cloud-native applications. Serverless functions, running in cloud environments, allow developers to focus on writing code without managing infrastructure. These functions respond to events such as HTTP requests or database changes and automatically scale with demand.
# Fastify SWC TypeScript Serverless Function Boilerplate

## Scripts

Expand Down Expand Up @@ -54,119 +46,20 @@ pnpm start

### Main Dependencies

- [`fastify`](https://www.npmjs.com/package/fastify): A fast and low-overhead web framework for Node.js, built for efficiency and scalability.
- [`fastify`](https://www.npmjs.com/package/fastify)

### Development Dependencies

- [`@swc-node/jest`](https://www.npmjs.com/package/@swc-node/jest): A Jest transformer using SWC for efficient testing with TypeScript.
- [`@swc-node/register`](https://www.npmjs.com/package/@swc-node/register): Allows on-the-fly transpilation of TypeScript to JavaScript using SWC during development.
- [`@swc/cli`](https://www.npmjs.com/package/@swc/cli): The command-line interface for the SWC transpiler.
- [`@swc/core`](https://www.npmjs.com/package/@swc/core): The core library for SWC, used for fast TypeScript and JavaScript transpilation.
- [`@swc/helpers`](https://www.npmjs.com/package/@swc/helpers): Helper functions used by SWC to support advanced JavaScript features.
- [`@types/jest`](https://www.npmjs.com/package/@types/jest): TypeScript definitions for Jest, ensuring strong typing in test files.
- [`@types/node`](https://www.npmjs.com/package/@types/node): TypeScript definitions for Node.js APIs.
- [`jest`](https://www.npmjs.com/package/jest): A comprehensive testing framework for JavaScript and TypeScript.
- [`nodemon`](https://www.npmjs.com/package/nodemon): Monitors for changes in the source and automatically restarts the server in development mode.
- [`ts-node`](https://www.npmjs.com/package/ts-node): A TypeScript execution environment for Node.js, used here for interpreting the TypeScript Jest configuration file.

## Serverless Functions Support (Fastify AWS Lambda)

This project is compatible with AWS Lambda through the [`@fastify/aws-lambda`](https://github.com/fastify/aws-lambda-fastify) package, allowing you to build Fastify applications that can be deployed as serverless functions.

### Installation

Before setting up your AWS Lambda integration, install the necessary package using:

```bash
pnpm i @fastify/aws-lambda
```

**Key Benefits:**

- **Fast Execution:** Fastify's inject function and `@fastify/aws-lambda`'s optimizations result in quicker performance.
- **Modular Architecture:** Easily build and extend Fastify applications as serverless functions.
- **Seamless Integration:** Fastify works smoothly with AWS Lambda and API Gateway, scaling automatically with demand.

### Example Fastify AWS Lambda Setup

To set up a Fastify app for AWS Lambda, modify your `src/index.ts` and create a Lambda handler file (`lambda.ts`).

1. **Fastify App (`src/index.ts`)**:

```typescript
import fastify, { FastifyReply, FastifyRequest } from "fastify";

const app = fastify();

app.get("/", async (request: FastifyRequest, reply: FastifyReply) => {
return { hello: "world" };
});

// ES Module alternative to require.main === module
if (import.meta.url === `file://${process.argv[1]}`) {
app.listen({ port: 3000 }, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log("Server listening on port 3000");
});
}

export default app; // Export the Fastify app for AWS Lambda
```

2. **Lambda Handler (`lambda.ts`)**:

```typescript
import awsLambdaFastify from "@fastify/aws-lambda";
import app from "./src/index.js";

const proxy = awsLambdaFastify(app);

export const handler = proxy;
```

3. **Deploy**: Deploy your Fastify app using AWS Lambda via a service such as Serverless Framework, AWS SAM, or AWS CDK.

### Using Lambda Arguments in Fastify

Fastify passes the original Lambda event and context in requests by default. Access them like this:

```typescript
app.get("/", (request: FastifyRequest, reply: FastifyReply) => {
const event = request.awsLambda.event;
const context = request.awsLambda.context;
reply.send({ event, context });
});
```

### Optimizing Cold Starts (In `lambda.ts`)

To lower cold start latency with AWS Lambda and Node.js 14+, you can use top-level `await` in combination with `fastify.ready()`:

```typescript
import awsLambdaFastify from "@fastify/aws-lambda";
import app from "./src/index.js";

export const handler = awsLambdaFastify(app);
await app.ready(); // Optimize cold starts
```

<img width="300" alt="logo" src="https://github.com/user-attachments/assets/a6907512-87a4-45de-9188-cdc494dfe5a8">

## Licensing

This project is licensed under the MIT License.

### Third-Party Licenses

The repository uses some third-party dependencies under other licenses:

- Apache-2.0
- ISC

The full texts of these licenses are included in the repository.
- [`@swc-node/jest`](https://www.npmjs.com/package/@swc-node/jest)
- [`@swc-node/register`](https://www.npmjs.com/package/@swc-node/register)
- [`@swc/cli`](https://www.npmjs.com/package/@swc/cli)
- [`@swc/core`](https://www.npmjs.com/package/@swc/core)
- [`@swc/helpers`](https://www.npmjs.com/package/@swc/helpers)
- [`@types/jest`](https://www.npmjs.com/package/@types/jest)
- [`@types/node`](https://www.npmjs.com/package/@types/node)
- [`jest`](https://www.npmjs.com/package/jest)
- [`nodemon`](https://www.npmjs.com/package/nodemon)
- [`ts-node`](https://www.npmjs.com/package/ts-node)

## Contributions and Issues

Expand Down