Skip to content

Commit

Permalink
lint: use es6 everywhere besides legacy api docs
Browse files Browse the repository at this point in the history
closes #470
  • Loading branch information
dougwilson committed Feb 15, 2022
1 parent df56ec3 commit 1fd61c6
Show file tree
Hide file tree
Showing 81 changed files with 645 additions and 647 deletions.
2 changes: 1 addition & 1 deletion _includes/api/en/5x/app-all.md
Expand Up @@ -11,7 +11,7 @@ The following callback is executed for requests to `/secret` whether using
GET, POST, PUT, DELETE, or any other HTTP request method:

```js
app.all('/secret', function (req, res, next) {
app.all('/secret', (req, res, next) => {
console.log('Accessing the secret section ...')
next() // pass control to the next handler
})
Expand Down
2 changes: 1 addition & 1 deletion _includes/api/en/5x/app-delete-method.md
Expand Up @@ -8,7 +8,7 @@ For more information, see the [routing guide](/guide/routing.html).
#### Example

```js
app.delete('/', function (req, res) {
app.delete('/', (req, res) => {
res.send('DELETE request to homepage')
})
```
2 changes: 1 addition & 1 deletion _includes/api/en/5x/app-get-method.md
Expand Up @@ -9,7 +9,7 @@ For more information, see the [routing guide](/guide/routing.html).
#### Example

```js
app.get('/', function (req, res) {
app.get('/', (req, res) => {
res.send('GET request to homepage')
})
```
6 changes: 3 additions & 3 deletions _includes/api/en/5x/app-mountpath.md
Expand Up @@ -12,7 +12,7 @@ const express = require('express')
const app = express() // the main app
const admin = express() // the sub app

admin.get('/', function (req, res) {
admin.get('/', (req, res) => {
console.log(admin.mountpath) // /admin
res.send('Admin Homepage')
})
Expand All @@ -29,13 +29,13 @@ patterns it is mounted on, as shown in the following example.
```js
const admin = express()

admin.get('/', function (req, res) {
admin.get('/', (req, res) => {
console.log(admin.mountpath) // [ '/adm*n', '/manager' ]
res.send('Admin Homepage')
})

const secret = express()
secret.get('/', function (req, res) {
secret.get('/', (req, res) => {
console.log(secret.mountpath) // /secr*t
res.send('Admin Secret')
})
Expand Down
4 changes: 2 additions & 2 deletions _includes/api/en/5x/app-onmount.md
Expand Up @@ -16,12 +16,12 @@ For details, see [Application settings](/en/5x/api.html#app.settings.table).
```js
const admin = express()

admin.on('mount', function (parent) {
admin.on('mount', (parent) => {
console.log('Admin Mounted')
console.log(parent) // refers to the parent app
})

admin.get('/', function (req, res) {
admin.get('/', (req, res) => {
res.send('Admin Homepage')
})

Expand Down
16 changes: 8 additions & 8 deletions _includes/api/en/5x/app-param.md
Expand Up @@ -7,9 +7,9 @@ If `name` is an array, the `callback` trigger is registered for each parameter d
For example, when `:user` is present in a route path, you may map user loading logic to automatically provide `req.user` to the route, or perform validations on the parameter input.

```js
app.param('user', function (req, res, next, id) {
app.param('user', (req, res, next, id) => {
// try to get the user details from the User model and attach it to the request object
User.find(id, function (err, user) {
User.find(id, (err, user) => {
if (err) {
next(err)
} else if (user) {
Expand All @@ -27,17 +27,17 @@ Param callback functions are local to the router on which they are defined. They
All param callbacks will be called before any handler of any route in which the param occurs, and they will each be called only once in a request-response cycle, even if the parameter is matched in multiple routes, as shown in the following examples.

```js
app.param('id', function (req, res, next, id) {
app.param('id', (req, res, next, id) => {
console.log('CALLED ONLY ONCE')
next()
})

app.get('/user/:id', function (req, res, next) {
app.get('/user/:id', (req, res, next) => {
console.log('although this matches')
next()
})

app.get('/user/:id', function (req, res) {
app.get('/user/:id', (req, res) => {
console.log('and this matches too')
res.end()
})
Expand All @@ -52,17 +52,17 @@ and this matches too
```

```js
app.param(['id', 'page'], function (req, res, next, value) {
app.param(['id', 'page'], (req, res, next, value) => {
console.log('CALLED ONLY ONCE with', value)
next()
})

app.get('/user/:id/:page', function (req, res, next) {
app.get('/user/:id/:page', (req, res, next) => {
console.log('although this matches')
next()
})

app.get('/user/:id/:page', function (req, res) {
app.get('/user/:id/:page', (req, res) => {
console.log('and this matches too')
res.end()
})
Expand Down
2 changes: 1 addition & 1 deletion _includes/api/en/5x/app-post-method.md
Expand Up @@ -8,7 +8,7 @@ For more information, see the [routing guide](/guide/routing.html).
#### Example

```js
app.post('/', function (req, res) {
app.post('/', (req, res) => {
res.send('POST request to homepage')
})
```
2 changes: 1 addition & 1 deletion _includes/api/en/5x/app-put-method.md
Expand Up @@ -7,7 +7,7 @@ Routes HTTP PUT requests to the specified path with the specified callback funct
#### Example

```js
app.put('/', function (req, res) {
app.put('/', (req, res) => {
res.send('PUT request to homepage')
})
```
4 changes: 2 additions & 2 deletions _includes/api/en/5x/app-render.md
Expand Up @@ -15,11 +15,11 @@ cache view during development; view caching is enabled in production by default.
</div>

```js
app.render('email', function (err, html) {
app.render('email', (err, html) => {
// ...
})

app.render('email', { name: 'Tobi' }, function (err, html) {
app.render('email', { name: 'Tobi' }, (err, html) => {
// ...
})
```
6 changes: 3 additions & 3 deletions _includes/api/en/5x/app-route.md
Expand Up @@ -7,14 +7,14 @@ Use `app.route()` to avoid duplicate route names (and thus typo errors).
const app = express()

app.route('/events')
.all(function (req, res, next) {
.all((req, res, next) => {
// runs for all HTTP verbs first
// think of it as route specific middleware!
})
.get(function (req, res, next) {
.get((req, res, next) => {
res.json({})
})
.post(function (req, res, next) {
.post((req, res, next) => {
// maybe add a new event...
})
```
2 changes: 1 addition & 1 deletion _includes/api/en/5x/app-router.md
Expand Up @@ -7,7 +7,7 @@ const express = require('express')
const app = express()
const router = app.router

router.get('/', function (req, res) {
router.get('/', (req, res) => {
res.send('hello world')
})

Expand Down
4 changes: 2 additions & 2 deletions _includes/api/en/5x/app-settings.md
Expand Up @@ -263,7 +263,7 @@ app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal'])
Custom trust implementation. Use this only if you know what you are doing.

```js
app.set('trust proxy', function (ip) {
app.set('trust proxy', (ip) => {
if (ip === '127.0.0.1' || ip === '123.123.123.123') return true // trusted IPs
else return false
})
Expand Down Expand Up @@ -308,7 +308,7 @@ The [express.static](#express.static) middleware ignores these settings.
<td markdown="1">Custom ETag function implementation. Use this only if you know what you are doing.

```js
app.set('etag', function (body, encoding) {
app.set('etag', (body, encoding) => {
return generateHash(body, encoding) // consider the function is defined
})
```
Expand Down
42 changes: 21 additions & 21 deletions _includes/api/en/5x/app-use.md
Expand Up @@ -16,7 +16,7 @@ Since `path` defaults to "/", middleware mounted without a path will be executed
For example, this middleware function will be executed for _every_ request to the app:

```js
app.use(function (req, res, next) {
app.use((req, res, next) => {
console.log('Time: %d', Date.now())
next()
})
Expand All @@ -37,12 +37,12 @@ Middleware functions are executed sequentially, therefore the order of middlewar

```js
// this middleware will not allow the request to go beyond it
app.use(function (req, res, next) {
app.use((req, res, next) => {
res.send('Hello World')
})

// requests will never reach this route
app.get('/', function (req, res) {
app.get('/', (req, res) => {
res.send('Welcome')
})
```
Expand All @@ -54,7 +54,7 @@ Error-handling middleware always takes _four_ arguments. You must provide four
Define error-handling middleware functions in the same way as other middleware functions, except with four arguments instead of three, specifically with the signature `(err, req, res, next)`):

```js
app.use(function (err, req, res, next) {
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).send('Something broke!')
})
Expand Down Expand Up @@ -83,7 +83,7 @@ mounting middleware.
This will match paths starting with `/abcd`:

```js
app.use('/abcd', function (req, res, next) {
app.use('/abcd', (req, res, next) => {
next()
})
```
Expand All @@ -97,31 +97,31 @@ app.use('/abcd', function (req, res, next) {
This will match paths starting with `/abcd` and `/abd`:

```js
app.use('/abc?d', function (req, res, next) {
app.use('/abc?d', (req, res, next) => {
next()
})
```

This will match paths starting with `/abcd`, `/abbcd`, `/abbbbbcd`, and so on:

```js
app.use('/ab+cd', function (req, res, next) {
app.use('/ab+cd', (req, res, next) => {
next()
})
```

This will match paths starting with `/abcd`, `/abxcd`, `/abFOOcd`, `/abbArcd`, and so on:

```js
app.use('/ab*cd', function (req, res, next) {
app.use('/ab*cd', (req, res, next) => {
next()
})
```

This will match paths starting with `/ad` and `/abcd`:

```js
app.use('/a(bc)?d', function (req, res, next) {
app.use('/a(bc)?d', (req, res, next) => {
next()
})
```
Expand All @@ -135,7 +135,7 @@ app.use('/a(bc)?d', function (req, res, next) {
This will match paths starting with `/abc` and `/xyz`:

```js
app.use(/\/abc|\/xyz/, function (req, res, next) {
app.use(/\/abc|\/xyz/, (req, res, next) => {
next()
})
```
Expand All @@ -149,7 +149,7 @@ app.use(/\/abc|\/xyz/, function (req, res, next) {
This will match paths starting with `/abcd`, `/xyza`, `/lmn`, and `/pqr`:

```js
app.use(['/abcd', '/xyza', /\/lmn|\/pqr/], function (req, res, next) {
app.use(['/abcd', '/xyza', /\/lmn|\/pqr/], (req, res, next) => {
next()
})
```
Expand Down Expand Up @@ -184,7 +184,7 @@ Even though the examples are for `app.use()`, they are also valid for `app.use()
You can define and mount a middleware function locally.

```js
app.use(function (req, res, next) {
app.use((req, res, next) => {
next()
})
```
Expand All @@ -193,7 +193,7 @@ A router is valid middleware.

```js
const router = express.Router()
router.get('/', function (req, res, next) {
router.get('/', (req, res, next) => {
next()
})
app.use(router)
Expand All @@ -202,7 +202,7 @@ app.use(router)
An Express app is valid middleware.
```js
const subApp = express()
subApp.get('/', function (req, res, next) {
subApp.get('/', (req, res, next) => {
next()
})
app.use(subApp)
Expand All @@ -218,12 +218,12 @@ You can specify more than one middleware function at the same mount path.

```js
const r1 = express.Router()
r1.get('/', function (req, res, next) {
r1.get('/', (req, res, next) => {
next()
})

const r2 = express.Router()
r2.get('/', function (req, res, next) {
r2.get('/', (req, res, next) => {
next()
})

Expand All @@ -240,12 +240,12 @@ Use an array to group middleware logically.

```js
const r1 = express.Router()
r1.get('/', function (req, res, next) {
r1.get('/', (req, res, next) => {
next()
})

const r2 = express.Router()
r2.get('/', function (req, res, next) {
r2.get('/', (req, res, next) => {
next()
})

Expand All @@ -265,13 +265,13 @@ function mw1 (req, res, next) { next() }
function mw2 (req, res, next) { next() }

const r1 = express.Router()
r1.get('/', function (req, res, next) { next() })
r1.get('/', (req, res, next) => { next() })

const r2 = express.Router()
r2.get('/', function (req, res, next) { next() })
r2.get('/', (req, res, next) => { next() })

const subApp = express()
subApp.get('/', function (req, res, next) { next() })
subApp.get('/', (req, res, next) => { next() })

app.use(mw1, [mw2, r1, r2], subApp)
```
Expand Down
2 changes: 1 addition & 1 deletion _includes/api/en/5x/app.md
Expand Up @@ -7,7 +7,7 @@ Create it by calling the top-level `express()` function exported by the Express
const express = require('express')
const app = express()

app.get('/', function (req, res) {
app.get('/', (req, res) => {
res.send('hello world')
})

Expand Down
2 changes: 1 addition & 1 deletion _includes/api/en/5x/express.static.md
Expand Up @@ -79,7 +79,7 @@ const options = {
index: false,
maxAge: '1d',
redirect: false,
setHeaders: function (res, path, stat) {
setHeaders (res, path, stat) {
res.set('x-timestamp', Date.now())
}
}
Expand Down
4 changes: 2 additions & 2 deletions _includes/api/en/5x/req-app.md
Expand Up @@ -14,7 +14,7 @@ app.get('/viewdirectory', require('./mymiddleware.js'))

```js
// mymiddleware.js
module.exports = function (req, res) {
res.send('The views directory is ' + req.app.get('views'))
module.exports = (req, res) => {
res.send(`The views directory is ${req.app.get('views')}`)
}
```

0 comments on commit 1fd61c6

Please sign in to comment.