Skip to content

Commit 0950131

Browse files
authored
Add ES6 example to the README.md (#186)
1 parent b653dbd commit 0950131

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ First declare `express-html-validator` as a dependency in your app.
1111

1212
Then require the package into your application and call its constructor, passing along your Express app:
1313

14+
Usage with CommonJS:
15+
1416
```js
1517
const express = require('express')
1618
const expressValidator = require('express-html-validator')
@@ -29,7 +31,30 @@ app.get('/', (req, res) => {
2931
})
3032
```
3133

32-
You can also run the validator on arbitrary strings outide of the Express context:
34+
Usage with ES modules:
35+
36+
```js
37+
import express from 'express';
38+
import expressValidator from 'express-html-validator'
39+
import { fileURLToPath } from 'url'
40+
import { dirname } from 'path'
41+
const router = express.Router();
42+
const app = express()
43+
const __filename = fileURLToPath(import.meta.url)
44+
const __dirname = dirname(__filename)
45+
46+
// Generally this would be used in development mode
47+
if (process.env.NODE_ENV === 'development') {
48+
expressValidator(app, config)
49+
}
50+
51+
// expressValidator should be called before defining routes
52+
router.get('/', (req, res) => {
53+
// This html response will be validated in real time as it's sent
54+
res.sendFile(__dirname + '/index.html');
55+
})
56+
```
57+
You can also run the validator on arbitrary strings outide of the Express context (example in CommonJS):
3358
3459
```js
3560
const config = {}
@@ -80,3 +105,4 @@ Optionally you can pass this module a set of configs:
80105
"extends": ["html-validate:standard"]
81106
}
82107
```
108+

0 commit comments

Comments
 (0)