简体   繁体   English

Node Express 动态路由/路径

[英]Node Express dynamic route/path

How can I implement and get a dynamic route or path with Express package?如何使用 Express package 实现和获取动态路由或路径? The main problem is the path is an id pass by the client and had no control over it.主要问题是路径是客户端的 id 传递并且无法控制它。

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 .以上将导致https://my-app.net/v1/user和客户端请求将是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.我需要允许动态路径,并且还需要获取user_id的值以供将来使用。

Added :user_id to the route.:user_id添加到路由中。

dynamic_path.get('/user/:user_id', (req, res) => {
  const user_id = req.params.user_id;
});
use route 
https://my-app.net/v1/user/:user_id
code will be like this 
dynamic_path.get("/user/:user_id" , (req, res)=>{

let user_id = req.parmas.user_id

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM