Javascript 获取节点js中的路由参数

Javascript 获取节点js中的路由参数,javascript,Javascript,我正在NodeJS中构建一个RESTAPI服务器。假设我有一个Api-http://localhost:3000/api/employee/:employeeId. 我想在不使用任何框架(如expressjs)的情况下获取employeeId参数。如果可能的话,有什么更好的方法呢?这里有一个普通的js函数来获取url段 function UrlSegments() { // Change this to the correct url if its not // the url

我正在NodeJS中构建一个RESTAPI服务器。假设我有一个Api-http://localhost:3000/api/employee/:employeeId. 我想在不使用任何框架(如expressjs)的情况下获取employeeId参数。如果可能的话,有什么更好的方法呢?

这里有一个普通的js函数来获取url段

function UrlSegments() {
    // Change this to the correct url if its not 
    // the url that you want to process
    // You can also do window.location.pathname to process
    // the current path.
    let urlSegments: string[] = /api/employee/:employeeId

    // Remove the empty string before the first '/'
    if (urlSegments.shift() === "")
        urlSegments.slice(1, -1);
    // Remove the empty string after the last '/'
    if (urlSegments.slice(-1)[0] === "")
        urlSegments.splice(-1, 1);

    return urlSegments;
}   

string employeeId = UrlSegments()[2];

**如果您需要Url中的员工Id,以便可以使用此-**

const express=需要“express”; const router=express.router

get,getUserById

getUserByIdreq,res{

   console.log('---EmployeeId ----',req.params.employeeId);
}

字符串拆分怎么样?让url=localhost:3000/api/employee/:employeeId,urlSegments=url.split'/';然后只参考urlSegments[3]对于url段数组中的最后一项。不确定您为什么不想使用express,但查看可能会给您一些HintExpressJS在其路由中的隐藏用途。您可以直接使用。RE:如果可能,有什么更好的方法?express已成为Node.js的实际标准web框架。没有更好的方法oach.其他任何东西都会少…经过战斗考验。