Skip to content

Commit 4ddb4b7

Browse files
committed
use neostandard() eslint config
1 parent c5c272c commit 4ddb4b7

File tree

143 files changed

+8766
-8863
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+8766
-8863
lines changed

benchmarks/middleware.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
2-
var express = require('..');
3-
var app = express();
1+
const express = require('../')
2+
const app = express()
43

54
// number of middleware
65

7-
var n = parseInt(process.env.MW || '1', 10);
8-
console.log(' %s middleware', n);
6+
let n = parseInt(process.env.MW || '1', 10)
7+
console.log(' %s middleware', n)
98

109
while (n--) {
11-
app.use(function(req, res, next){
12-
next();
13-
});
10+
app.use((req, res, next) => {
11+
next()
12+
})
1413
}
1514

16-
app.use(function(req, res){
15+
app.use((req, res) => {
1716
res.send('Hello World')
18-
});
17+
})
1918

20-
app.listen(3333);
19+
app.listen(3333)

eslint.config.mjs

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
1-
import globals from "globals";
2-
3-
export default [{
4-
ignores: ["**/coverage", "**/node_modules"],
5-
}, {
6-
languageOptions: {
7-
globals: {
8-
...globals.node,
9-
},
10-
},
11-
12-
rules: {
13-
"eol-last": "error",
14-
eqeqeq: ["error", "allow-null"],
15-
16-
indent: ["error", 2, {
17-
MemberExpression: "off",
18-
SwitchCase: 1,
19-
}],
20-
21-
"no-trailing-spaces": "error",
22-
23-
"no-unused-vars": ["error", {
24-
vars: "all",
25-
args: "none",
26-
caughtErrors: "none",
27-
ignoreRestSiblings: true,
28-
}],
29-
30-
"no-restricted-globals": ["error", {
31-
name: "Buffer",
32-
message: "Use `import { Buffer } from \"node:buffer\"` instead of the global Buffer.",
33-
}],
34-
},
35-
}];
1+
import neostandard from 'neostandard'
2+
3+
export default [
4+
...neostandard({
5+
env: ['mocha', 'node']
6+
}),
7+
{
8+
rules: {
9+
'global-require': 'warn',
10+
'n/handle-callback-err': 'off'
11+
}
12+
}
13+
]

examples/auth/index.js

Lines changed: 66 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
* Module dependencies.
55
*/
66

7-
var express = require('../..');
8-
var hash = require('pbkdf2-password')()
9-
var path = require('node:path');
10-
var session = require('express-session');
7+
const express = require('../../')
8+
const hash = require('pbkdf2-password')()
9+
const path = require('node:path')
10+
const session = require('express-session')
1111

12-
var app = module.exports = express();
12+
const app = module.exports = express()
1313

1414
// config
1515

16-
app.set('view engine', 'ejs');
17-
app.set('views', path.join(__dirname, 'views'));
16+
app.set('view engine', 'ejs')
17+
app.set('views', path.join(__dirname, 'views'))
1818

1919
// middleware
2020

@@ -23,112 +23,111 @@ app.use(session({
2323
resave: false, // don't save session if unmodified
2424
saveUninitialized: false, // don't create session until something stored
2525
secret: 'shhhh, very secret'
26-
}));
26+
}))
2727

2828
// Session-persisted message middleware
2929

30-
app.use(function(req, res, next){
31-
var err = req.session.error;
32-
var msg = req.session.success;
33-
delete req.session.error;
34-
delete req.session.success;
35-
res.locals.message = '';
36-
if (err) res.locals.message = '<p class="msg error">' + err + '</p>';
37-
if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';
38-
next();
39-
});
30+
app.use((req, res, next) => {
31+
const err = req.session.error
32+
const msg = req.session.success
33+
delete req.session.error
34+
delete req.session.success
35+
res.locals.message = ''
36+
if (err) res.locals.message = '<p class="msg error">' + err + '</p>'
37+
if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>'
38+
next()
39+
})
4040

4141
// dummy database
4242

43-
var users = {
43+
const users = {
4444
tj: { name: 'tj' }
45-
};
45+
}
4646

4747
// when you create a user, generate a salt
4848
// and hash the password ('foobar' is the pass here)
4949

50-
hash({ password: 'foobar' }, function (err, pass, salt, hash) {
51-
if (err) throw err;
50+
hash({ password: 'foobar' }, (err, pass, salt, hash) => {
51+
if (err) throw err
5252
// store the salt & hash in the "db"
53-
users.tj.salt = salt;
54-
users.tj.hash = hash;
55-
});
56-
53+
users.tj.salt = salt
54+
users.tj.hash = hash
55+
})
5756

5857
// Authenticate using our plain-object database of doom!
5958

60-
function authenticate(name, pass, fn) {
61-
if (!module.parent) console.log('authenticating %s:%s', name, pass);
62-
var user = users[name];
59+
function authenticate (name, pass, fn) {
60+
if (!module.parent) console.log('authenticating %s:%s', name, pass)
61+
const user = users[name]
6362
// query the db for the given username
6463
if (!user) return fn(null, null)
6564
// apply the same algorithm to the POSTed password, applying
6665
// the hash against the pass / salt, if there is a match we
6766
// found the user
68-
hash({ password: pass, salt: user.salt }, function (err, pass, salt, hash) {
69-
if (err) return fn(err);
67+
hash({ password: pass, salt: user.salt }, (err, pass, salt, hash) => {
68+
if (err) return fn(err)
7069
if (hash === user.hash) return fn(null, user)
7170
fn(null, null)
72-
});
71+
})
7372
}
7473

75-
function restrict(req, res, next) {
74+
function restrict (req, res, next) {
7675
if (req.session.user) {
77-
next();
76+
next()
7877
} else {
79-
req.session.error = 'Access denied!';
80-
res.redirect('/login');
78+
req.session.error = 'Access denied!'
79+
res.redirect('/login')
8180
}
8281
}
8382

84-
app.get('/', function(req, res){
85-
res.redirect('/login');
86-
});
83+
app.get('/', (req, res) => {
84+
res.redirect('/login')
85+
})
8786

88-
app.get('/restricted', restrict, function(req, res){
89-
res.send('Wahoo! restricted area, click to <a href="/logout">logout</a>');
90-
});
87+
app.get('/restricted', restrict, (req, res) => {
88+
res.send('Wahoo! restricted area, click to <a href="/logout">logout</a>')
89+
})
9190

92-
app.get('/logout', function(req, res){
91+
app.get('/logout', (req, res) => {
9392
// destroy the user's session to log them out
9493
// will be re-created next request
95-
req.session.destroy(function(){
96-
res.redirect('/');
97-
});
98-
});
94+
req.session.destroy(() => {
95+
res.redirect('/')
96+
})
97+
})
9998

100-
app.get('/login', function(req, res){
101-
res.render('login');
102-
});
99+
app.get('/login', (req, res) => {
100+
res.render('login')
101+
})
103102

104-
app.post('/login', function (req, res, next) {
103+
app.post('/login', (req, res, next) => {
105104
if (!req.body) return res.sendStatus(400)
106-
authenticate(req.body.username, req.body.password, function(err, user){
105+
authenticate(req.body.username, req.body.password, (err, user) => {
107106
if (err) return next(err)
108107
if (user) {
109108
// Regenerate session when signing in
110109
// to prevent fixation
111-
req.session.regenerate(function(){
110+
req.session.regenerate(() => {
112111
// Store the user's primary key
113112
// in the session store to be retrieved,
114113
// or in this case the entire user object
115-
req.session.user = user;
116-
req.session.success = 'Authenticated as ' + user.name
117-
+ ' click to <a href="/logout">logout</a>. '
118-
+ ' You may now access <a href="/restricted">/restricted</a>.';
119-
res.redirect(req.get('Referrer') || '/');
120-
});
114+
req.session.user = user
115+
req.session.success = 'Authenticated as ' + user.name +
116+
' click to <a href="/logout">logout</a>. ' +
117+
' You may now access <a href="/restricted">/restricted</a>.'
118+
res.redirect(req.get('Referrer') || '/')
119+
})
121120
} else {
122-
req.session.error = 'Authentication failed, please check your '
123-
+ ' username and password.'
124-
+ ' (use "tj" and "foobar")';
125-
res.redirect('/login');
121+
req.session.error = 'Authentication failed, please check your ' +
122+
' username and password.' +
123+
' (use "tj" and "foobar")'
124+
res.redirect('/login')
126125
}
127-
});
128-
});
126+
})
127+
})
129128

130129
/* istanbul ignore next */
131130
if (!module.parent) {
132-
app.listen(3000);
133-
console.log('Express started on port 3000');
131+
app.listen(3000)
132+
console.log('Express started on port 3000')
134133
}

examples/content-negotiation/db.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

3-
var users = [];
3+
const users = []
44

5-
users.push({ name: 'Tobi' });
6-
users.push({ name: 'Loki' });
7-
users.push({ name: 'Jane' });
5+
users.push({ name: 'Tobi' })
6+
users.push({ name: 'Loki' })
7+
users.push({ name: 'Jane' })
88

9-
module.exports = users;
9+
module.exports = users
Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,43 @@
11
'use strict'
22

3-
var express = require('../../');
4-
var app = module.exports = express();
5-
var users = require('./db');
3+
const express = require('../../')
4+
const app = module.exports = express()
5+
const users = require('./db')
66

77
// so either you can deal with different types of formatting
88
// for expected response in index.js
9-
app.get('/', function(req, res){
9+
app.get('/', (req, res) => {
1010
res.format({
11-
html: function(){
12-
res.send('<ul>' + users.map(function(user){
13-
return '<li>' + user.name + '</li>';
14-
}).join('') + '</ul>');
11+
html: function () {
12+
res.send('<ul>' + users.map((user) => '<li>' + user.name + '</li>').join('') + '</ul>')
1513
},
1614

17-
text: function(){
18-
res.send(users.map(function(user){
19-
return ' - ' + user.name + '\n';
20-
}).join(''));
15+
text: function () {
16+
res.send(users.map((user) => ' - ' + user.name + '\n').join(''))
2117
},
2218

23-
json: function(){
24-
res.json(users);
19+
json: function () {
20+
res.json(users)
2521
}
26-
});
27-
});
22+
})
23+
})
2824

2925
// or you could write a tiny middleware like
3026
// this to add a layer of abstraction
3127
// and make things a bit more declarative:
3228

33-
function format(path) {
34-
var obj = require(path);
35-
return function(req, res){
36-
res.format(obj);
37-
};
29+
function format (path) {
30+
// eslint-disable-next-line global-require
31+
const obj = require(path)
32+
return function (req, res) {
33+
res.format(obj)
34+
}
3835
}
3936

40-
app.get('/users', format('./users'));
37+
app.get('/users', format('./users'))
4138

4239
/* istanbul ignore next */
4340
if (!module.parent) {
44-
app.listen(3000);
45-
console.log('Express started on port 3000');
41+
app.listen(3000)
42+
console.log('Express started on port 3000')
4643
}
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
'use strict'
22

3-
var users = require('./db');
3+
const users = require('./db')
44

5-
exports.html = function(req, res){
6-
res.send('<ul>' + users.map(function(user){
7-
return '<li>' + user.name + '</li>';
8-
}).join('') + '</ul>');
9-
};
5+
exports.html = function (req, res) {
6+
res.send('<ul>' + users.map((user) => '<li>' + user.name + '</li>').join('') + '</ul>')
7+
}
108

11-
exports.text = function(req, res){
12-
res.send(users.map(function(user){
13-
return ' - ' + user.name + '\n';
14-
}).join(''));
15-
};
9+
exports.text = function (req, res) {
10+
res.send(users.map((user) => ' - ' + user.name + '\n').join(''))
11+
}
1612

17-
exports.json = function(req, res){
18-
res.json(users);
19-
};
13+
exports.json = function (req, res) {
14+
res.json(users)
15+
}

0 commit comments

Comments
 (0)