Skip to content

Commit

Permalink
docs(examples): Added Node.js Express server example (#333)
Browse files Browse the repository at this point in the history
- New example app showing usage of Arcjet with [Express](https://expressjs.com/).
- Added note to READMEs about the availability of `--env-file`
  • Loading branch information
davidmytton authored Mar 8, 2024
1 parent e0c2914 commit f398c28
Show file tree
Hide file tree
Showing 9 changed files with 882 additions and 10 deletions.
24 changes: 18 additions & 6 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,24 @@ updates:
versions: [">18.18"]

- package-ecosystem: npm
directory: /examples/node-rl
directory: /examples/nodejs-express-rl
schedule:
# Our dependencies should be checked daily
interval: daily
assignees:
- blaine-arcjet
reviewers:
- blaine-arcjet
commit-message:
prefix: deps(example)
prefix-development: deps(example)
groups:
dependencies:
patterns:
- "*"

- package-ecosystem: npm
directory: /examples/nodejs-rl
schedule:
# Our dependencies should be checked daily
interval: daily
Expand All @@ -236,8 +253,3 @@ updates:
dependencies:
patterns:
- "*"
ignore:
# Ignore updates to the @types/node package due to conflict between
# Headers in DOM.
- dependency-name: "@types/node"
versions: [">18.18"]
1 change: 1 addition & 0 deletions examples/nodejs-express-rl/.env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ARCJET_KEY=
42 changes: 42 additions & 0 deletions examples/nodejs-express-rl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<a href="https://arcjet.com" target="_arcjet-home">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://arcjet.com/arcjet-logo-minimal-dark-mark-all.svg">
<img src="https://arcjet.com/arcjet-logo-minimal-light-mark-all.svg" alt="Arcjet Logo" height="128" width="auto">
</picture>
</a>

# Arcjet Rate Limit with Express for Node.js

This example shows how to use Arcjet with a Node.js
[Express](https://expressjs.com/) server.

## How to use

1. From the root of the project, install the SDK dependencies.

```bash
npm ci
```

2. Enter this directory and install the example's dependencies.

```bash
cd examples/nodejs-express
npm ci
```

3. Rename `.env.local.example` to `.env.local` and add your Arcjet key.

4. Start the server.

```bash
npm start
```

This assumes you're using Node.js 20 or later because the `start` script
loads a local environment file with `--env-file`. If you're using an older
version of Node.js, you can use a package like
[dotenv](https://www.npmjs.com/package/dotenv) to load the environment file.

5. Visit `http://localhost:3000/`.
6. Refresh the page to trigger the rate limit.
38 changes: 38 additions & 0 deletions examples/nodejs-express-rl/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import arcjet, { fixedWindow } from "@arcjet/node";
import express from "express";

const app = express();
const port = 3000;

const aj = arcjet({
// Get your site key from https://app.arcjet.com and set it as an environment
// variable rather than hard coding.
key: process.env.ARCJET_KEY,
rules: [
// Fixed window rate limit. Arcjet also supports sliding window and token
// bucket.
fixedWindow({
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
// Limiting by ip.src is the default if not specified
//characteristics: ["ip.src"],
window: "1m", // 1 min fixed window
max: 1, // allow a single request (for demo purposes)
}),
],
});

app.get('/', async (req, res) => {
const decision = await aj.protect(req);

if (decision.isDenied()) {
res.writeHead(429, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Too Many Requests" }));
} else {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Hello World" }));
}
})

app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Loading

0 comments on commit f398c28

Please sign in to comment.