0
const express = require('express');
const app = express();

app.use('/', anyroute);

// in anyroute file
router.route('/:id').get(controlFunction)

controlFunction(req, res, res)=> {
// Here we can get the "id" from the variable with req.param.id
}

but I want to use the "id" before getting into this controller function. Like this

modifyFunction(userId)=> {
// Do something with the userId
}

router.route('/:id').get(modifyFunction(id), controlFunction)

Is it possible in express and javascript to retrieve the 'id' from url and use in modifyFunction(id) before getting into the request.

In an express middleware req and res variables are transferred from one middleware to next. But I created a function function(type, id) which returns a middleware function(req, res, next). in this function(type, id) type parameter will be passed manually and id from URL.

router
  .route('/all-contacts/:id')
  .get(authController.isLoggedIn, viewController.getAxiosConfig('contactNameEmail', id), viewController.getAllContact);  

exports.getAxiosConfig = (type, id) => {
  return (req, res, next) => {
    const config = axiosConfig(type, id);
    const accessToken = req.cookies.access_token;
    const tokenArray = config.headers.Authorization.split(' ');
    tokenArray.splice(1, 1, accessToken);
    const newToken = tokenArray.join(' ');
    config.headers.Authorization = newToken;
    req.config = config;
    next();
  };
};
exports.getAllContact = catchAsync(async (req, res, next) => {
  // const token = req.cookies.access_token;
  // console.log(token);
  // const config = axiosConfig('contactNameEmail');
  req.config.method = 'GET';
  // const tokenArray = config.headers.Authorization.split(' ');
  // tokenArray.splice(1, 1, token);
  // const newToken = tokenArray.join(' ');
  // config.headers.Authorization = newToken;
  const contacts = await axios(req.config);
  console.log(req.config);

  const { data } = contacts;
  res.status(200).render('contactList', {
    title: 'All contacts',
    data
  });
});
Hossain
  • 1
  • 1
  • Could you explain more about your final goal? I don't see why you would want to do that. Do you mean that you want to create some middleware in which to do something with that ID? – Luis Orbaiceta Sep 21 '20 at 17:49
  • Yes I want to create a middleware to get rid of reusing same code in my every route handler function. – Hossain Sep 21 '20 at 18:25
  • Do you know how to write a middleware? https://stackoverflow.com/questions/5284340/what-is-node-js-connect-express-and-middleware?r=SearchResults – Luis Orbaiceta Sep 21 '20 at 19:03
  • In an express middleware req and res variables are transferred from one middleware to next. But I created a function function(type, id) which returns a middleware function(req, res, next). in this function(type, id) type parameter will be passed manually and id from URL. – Hossain Sep 22 '20 at 01:10
  • So basically you want to get the I'd from the URL? – Luis Orbaiceta Sep 22 '20 at 05:05
  • Yes, from front-end using javascript and from back-end using express. – Hossain Sep 22 '20 at 07:54
  • Does this answer your question? https://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-express-js-on-node-js – Luis Orbaiceta Sep 22 '20 at 08:14
  • 1
    Thank you for your suggestion. Although I haven't found my answer but that link help me solve my problem. It's Ok now. Thanks again. – Hossain Sep 22 '20 at 11:21

1 Answers1

0

You can access the ID in the middleware, which is called before the route handler.

Kiran
  • 54
  • 3