I have a file with the following code:
const logger = (options) => (req, res, next) => {
if (typeof options === 'object'
&& options !== null
&& options.enable) {
console.log(
'Status Code:', res.statusCode,
'URL:', req.originalUrl,
)
}
next()
}
module.exports = logger
It is used as such in a different .js file:
const express = require('express')
const loggerMiddleware = require('./middleware-logger')
const app = express()
app.use(loggerMiddleware({
enable: true,
}))
app.listen(
1337,
() => console.log('Web Server listening on 1337'),
)
Can anyone explain what is happening with the series of => in the first line? I understand a normal (req,res,next) => {} to mean something like:
"There is an unnamed function with 3 arguments defined by what is in the curly braces.
In this case with the series of arrow operators, what I do not "get" is how the function def can see all the arguments in both (options) and (req,res,next) at the same time.
What is really going on here under the hood? Does this have something to do with promises? And again, how can the one method def see all the arguments at the same time?