Node Express dynamic route/path

How can I implement and get a dynamic route or path with Express package? The main problem is the path is an id pass by the client and had no control over it.

const express = require('express');
const dynamic_path= express();


dynamic_path.get('/user', (req, res) => {

});

exports.v1 = functions.runWith(runtimeOpts).https.onRequest(dynamic_path);

The above will result as https://my-app.net/v1/user and the client request will be https://my-app.net/v1/user/user_id. I need to allow dynamic path and I need to get the value of user_id as well for future usage.

>Solution :

Added :user_id to the route.

dynamic_path.get('/user/:user_id', (req, res) => {
  const user_id = req.params.user_id;
});

Leave a Reply