javascript - Best way to pass route params between callbacks in expressjs -
i've tried search similar problem on here suprisingly couldn't find 1 posted already.
i use expressjs v4 framework , i'm constructing routes this:
'use strict'; let express = require('express'); let router = express.router(); let users = require('./modules/users'); router.post('/',users.add); router.put('/edit/:id',users.edit);
as can see above, i'm requiring let users = require('./modules/users')
now users
module looks (let's say) this:
'use strict'; let usersdbmodule = require('...'); let users = { 'add': (req, res, next) => { let callback = (err, record) => { //...do users.function1(record) } usersdbmodule.save(req, callback); }, 'function1': (record) => { users.function2() }, 'function2': () => { //...do next() function } }
you can notice, router first code block using module's add
function. add
function it's standard express middleware function things getting more complicated.
as can see, add
function has next
1 of params, i'm doing complex callbacks calls different functions , let's in end want call next
in function2
.
my question is, best way of passing req
, res
, next
params between different callback functions within same module.
i come 3 different methods of doing it:
method 1: pass req
, res
or next
if necessary around functions in chain in case have pass next
callback
function1
, function1
function2
.
not best way in opinion, difficult maintain, read , test well.
method 2: wrap function1
, function2
closures in add
passing necessary params. in particular case have wrap function2
closure passing next
looks this:
'add': (req, res, next) => { users.function2(next); //....rest of code of function add }
and function2
itself:
'function2': (next) => {4 return () => { //...now have access next here // without need pass each , every // function in chain } }
method 3:
append necessary functions/variables res.locals
, pass res
object around.
it has same problem method 1
in favour of method 2
not sure if doesn't make code less readable , maybe there other problems it, haven't tested in production nor in development environment team.
i hear guys using , how plays in projects/teams. preferences, best practices, best patterns ? please share, want know what's best way.
maybe there better way of doing ?
all feedback appreciated!
real life example:
example usage function1
& function2
, possibly more...
let's have adapter fetches data external api, needs save data database, , return response. let's assume data returned api expires after 5s. if client hits route within 5s span, gets data database, if time between calls longer, repeats operation calling api.
this of course more complicated function1
, function2
. require lot of callback functions, both adapter , database, separate functions fetching data database, adapter, saving data database, , deleting data database, gives @ least 4 callback functions already.
i think mix express , app logic not idea. use next way in project
// middlewares/auth.js // example middleware exports.isadmin = function (req, res, next) { if (smth-admin-check) next(); else next (new error(403)); } // routes/index.js // include modules /routes let user = require('./user'); let auth = require('../middlewares/auth'); ... app.get('/user/:id(\\d+)', user.get); app.post('/user', auth.isadmin, user.post); // admin can add user // routes/user.js // call model methods , render/send data browser // don't know db let user = require('/models/user'); ... exports.get = function(req, res, next) { let id = req.params.id; // cache data in memory avoid callback-hell // in common case code below user.get(id, function(err, u) { if (!u) return next(new error('bad id')); ... render page or send json ... }); } ... exports.post = function(req, res, next) { ... } // models/user.js // encapsulate user logic // don't use express features let db = require('my-db'); ... class user { get(id, callback) { ... } add(data, callback) { ... } // return error or new user ... }
Comments
Post a Comment