-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(examples): Added Node.js Express server example (#333)
- 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
1 parent
e0c2914
commit f398c28
Showing
9 changed files
with
882 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ARCJET_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
}) |
Oops, something went wrong.