G Sis
G Sis

Reputation: 199

es6 harmony arrow functions in express handlers

Is there a reason not to use arrows instead of regular function expressions in expressjs for handlers in middleware?

app.use(mountSomething())
router.use(mountSomethingElse())

app.get('/', (req,res,next)=> { 
    next();
})

route.get('/path', (req,res,next)=>{
    res.send('send')
})

Upvotes: 20

Views: 8411

Answers (1)

QoP
QoP

Reputation: 28397

app.get('/', (req,res,next)=> { 
    next();
})

is the same as

app.get('/', function(req,res,next) { 
        next();
}.bind(this))

In most cases you are not going to use 'this'(which will be probably undefined) in the handlers, so you are free to use arrow functions.

Upvotes: 18

Related Questions