Skip to content
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

docs(examples): Added Node.js Express server example #333

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
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
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
Loading