-
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): Add Node.js express server validate email example (#343)
* 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
1 parent
a5db5a9
commit fc6c6a8
Showing
8 changed files
with
881 additions
and
1 deletion.
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
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,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. |
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,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}`) | ||
}) |
Oops, something went wrong.