Skip to content

Commit

Permalink
add Express Integration Example (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Nov 24, 2023
1 parent 56b330e commit df94643
Show file tree
Hide file tree
Showing 6 changed files with 698 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@mdx-js/mdx": "^2.3.0",
"@opentelemetry/exporter-trace-otlp-http": "0.43.0",
"@opentelemetry/sdk-trace-base": "^1.17.1",
"@types/express": "^4.17.21",
"@types/node": "^18.16.1",
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.1",
Expand All @@ -41,6 +42,7 @@
"eslint-plugin-mdx": "^2.0.5",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-unused-imports": "^3.0.0",
"express": "^4.18.2",
"next-sitemap": "^4.1.8",
"postcss": "^8.4.23",
"prettier": "^2.8.8",
Expand Down
5 changes: 5 additions & 0 deletions pages/docs/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"testing": "Testing",
"control-flow": "Control Flow",
"style": "Code Style",
"-Integrations": {
"title": "Integrations",
"type": "separator"
},
"express": "Express",
"-Concepts": {
"title": "Concepts",
"type": "separator"
Expand Down
103 changes: 103 additions & 0 deletions pages/docs/express.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Express Integration

In this guide, we'll explore how to integrate Effect with [Express](https://expressjs.com/), a popular web framework for Node.js.

## Hello World Example

Let's start with a simple example that creates an Express server responding with "Hello World!" for requests to the root URL (/).
This mirrors the classic ["Hello world example"](https://expressjs.com/en/starter/hello-world.html) found in the Express documentation.

### Setup Steps

1. Create a new directory for your project and navigate to it using your terminal:

```bash filename="Terminal"
mkdir express-effect-integration
cd express-effect-integration
```

2. Initialize your project with npm. This will create a `package.json` file:

```bash filename="Terminal"
npm init -y
```

3. Install the necessary dependencies:

```bash filename="Terminal"
npm install effect express
```

Install the necessary dev dependencies:

```bash
npm install typescript @types/express --save-dev
```

Now, initialize TypeScript:

```bash
npx tsc --init
```

4. Create a new file, for example, `hello-world.ts`, and add the following code:

```ts filename="hello-world.ts" file=<rootDir>/src/express/hello-world.ts

```

5. Run your Express server. If you have [`ts-node`](https://github.com/TypeStrong/ts-node) installed, run the following command in the terminal:

```bash filename="Terminal"
ts-node hello-world.ts
```

Visit [http://localhost:3001](http://localhost:3001) in your web browser, and you should see "Hello World!".

### Code Breakdown

Here's a breakdown of what's happening:

- **Express Service**. We define an `Express` [service](context-management/services.mdx) to retrieve the Express app later on.

```ts file=<rootDir>/src/express/hello-world.ts#L4-L5

```

- **Main Route**. The main route, `IndexRouteLive`, is defined as a [Layer](context-management/layers.mdx).

```ts file=<rootDir>/src/express/hello-world.ts#L7-L17

```

We access the [runtime](runtime.mdx) (`Effect.runtime`), which can be used to execute tasks within our route (`runFork`).
Since we don't need to produce any service in the output, we use `Layer.effectDiscard` to discard its output.

- **Server Setup**. The server is created in a layer (`ServerLive`) and mounted at the end of our program.

```ts file=<rootDir>/src/express/hello-world.ts#L19-L35

```

We use [`Effect.acquireRelease`](resource-management.mdx#defining-resources) to create the server, allowing automatic management of the [scope](resource-management.mdx#scope).
Again, as we don't need to produce any service in the output, we use `Layer.scopedDiscard` to discard its output.

- **Mounting**. Finally, we mount the server by adding our route

```ts {2} file=<rootDir>/src/express/hello-world.ts#L41-L44

```

and providing the necessary dependency to the Express app

```ts {6} file=<rootDir>/src/express/hello-world.ts#L38-L44

```

## Basic routing

In this example, we'll explore the basics of routing with Effect and Express. The goal is to create a simple web server with two routes: one that returns all todos and another that returns a todo by its ID.

```ts filename="basic-routing.ts" file=<rootDir>/src/express/basic-routing.ts

```
Loading

0 comments on commit df94643

Please sign in to comment.