Skip to content

Commit

Permalink
docs(examples): Add Node.js express server validate email example (#343)
Browse files Browse the repository at this point in the history
* docs(examples): Added Node.js express server validate email example

* Fix email

* Remove body parser as its built in

* Add dependabot config

* Update examples/nodejs-express-validate-email/index.js

---------

Co-authored-by: blaine-arcjet <[email protected]>
  • Loading branch information
davidmytton and blaine-arcjet authored Mar 11, 2024
1 parent a5db5a9 commit fc6c6a8
Show file tree
Hide file tree
Showing 8 changed files with 881 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,23 @@ updates:
patterns:
- "*"

- package-ecosystem: npm
directory: /examples/nodejs-express-validate-email
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:
Expand Down
2 changes: 1 addition & 1 deletion examples/nodejs-express-rl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This example shows how to use Arcjet with a Node.js
2. Enter this directory and install the example's dependencies.

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

Expand Down
1 change: 1 addition & 0 deletions examples/nodejs-express-validate-email/.env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ARCJET_KEY=
47 changes: 47 additions & 0 deletions examples/nodejs-express-validate-email/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<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 email verification 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-validate-email
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. Execute a curl request with an invalid email e.g.

```bash
curl -X POST -d '[email protected]' http://localhost:3000/
```

6. The request will be blocked and the server logs will show the details.
40 changes: 40 additions & 0 deletions examples/nodejs-express-validate-email/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import arcjet, { validateEmail } from "@arcjet/node";
import express from "express";

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

app.use(express.urlencoded({ extended: false }));

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: [
validateEmail({
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
block: ["NO_MX_RECORDS"], // block email addresses with no MX records
}),
],
});

app.post('/', async (req, res) => {
console.log("Email received: ", req.body.email)

const decision = await aj.protect(req, {
email: req.body.email,
});
console.log("Arcjet decision", decision);

if (decision.isDenied()) {
res.writeHead(403, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Forbidden" }));
} else {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Hello World", email: req.body.email }));
}
})

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

0 comments on commit fc6c6a8

Please sign in to comment.