Andzx
Andzx

Reputation: 25

How do I get the full route path including the parameters from express or extend the request object to do so?

I have the following route in my express (version 4.17.1) API in a postTimecardCompany.js file:

const mongoose = require('mongoose');
const Timecard = require('./../models/timecard');

function postTimecardCompany(server) {
    server.post('/api/v1/timecard/:userId', (req, res) => {    
        // Insert timecard data into the database
        Timecard.create(data, (error, result) => {
            // Check for errors
            if (error) {
                res.status(500).end();
                return;
            }
            
            // Respond
            res.status(200).send({timecardId: result._id});
        });
    });
}

module.exports = postTimecardCompany;

The route (among other routes) is loaded via the following mechanism by server.js file:

[   
    'postTimecardCompany',
    'anotherRoute',
    'someOtherRoute',
    'andSoOn...'
].map((route) => {
    require('./core/routes/' + route + '.js').call(null, server)
});

I have a middleware (in server.js file) where I check which route is being called.

server.use((req, res, next) => {
    // If route is "/api/v1/timecard/:userId" do something
});

I have found various solutions which do nearly what I am looking for, but not exactly. For example, if I post to the route with a data parameter userId value of "123f9b" then req.originalUrl gives an output of "/api/v1/timecard/123f9b."

What i'm looking to get is the original route path with the parameters in it so for a request of "/api/v1/timecard/123f9b" it would be: "/api/v1/timecard/:userId."

How do I get this functionality in express or extend express to get the original route path with parameters in the request object?

Upvotes: 1

Views: 1397

Answers (1)

Mohammad Yaser Ahmadi
Mohammad Yaser Ahmadi

Reputation: 5051

if you want to use from your approach, it's is impossible, after that your approach is not standard in express check the documentation, if you want get routes in a middleware you should try like this:

server.js

const express = require('express')
const server = express()
const postTimecardCompany = require('./routes/postTimecardCompany.js')// don't use map()
server.use("/",postTimecardCompany)//use the routes
server.listen(6565,()=>console.log(`Listening to PORT 6565`))

routes of postTimecardCompany.js

use Router of express and export router, and you can use middleware before each route you want, there are many ways to use middleware in routes, check the documentation

const express = require("express");
const router = express.Router();
const middleware = require('../middleware');//require middlewares

router.post("/api/v1/timecard/:userId", middleware,(req, res) => {
  // Insert timecard data into the database
  console.log(req.route.path);
});
module.exports = router;

middleware.js

module.exports = ((req, res, next) => {
    console.log(req.route.path);
    next()
});

Upvotes: 1

Related Questions