Laravel request->attribute->set equivalent in nodejs

Viewed 67

Migrating a piece of code from Laravel to Nodejs,

// Laravel
public function handle($request, Closure $next, ...$guards)
{
  $this->checkAndValidateToken($request);
  return $next($request);
}

protected function checkAndValidateToken(Request $request)
{
  // some code here
  $val = $request->$key ?? $request->$upperCaseKey;
  try {
    // some code here
    $request->attributes->set('someState', $this->myService->getToken($customer['email']));
  }
  catch(Exception $e) {}
}

My node equivalent so far is,

function handle(req, res) {
  checkAndValidateToken(req)
  return next(req)
}

async function checkAndValidateToken(req) {
  // some code here
  const val = req.query[key] ?? key.toUpperCase();

  try {
    // some code here
    const { data: { token } } = await myService.getToken(email)
    // stuck here, what is the equivalent for $req->attr->set?
  }
  catch(e) {}
}

I assume,

// Laravel
$val = $request->$key ?? $request.upperCaseKey;

Will be,

//Node.js
const val = req.query[key] ?? key.toUpperCase();

What is the $request->attributes->set equivalent in nodejs? since it just calls $next($req) after this.

0 Answers
Related