如何在Node.js中检索服务器端URL的可选参数

时间:2018-06-16 15:47:28

标签: javascript node.js angular

以下是带有Angular中参数的get请求:

  getRequestWithParameters(paramOne , paramTwo) : Observable<any>{
    return this.http.get(`http://localhost:5051/params/${paramOne}/${paramTwo}`).pipe(map(this.dataHandler));
  }

以下是基于尝试路由的路径名的代码:

function extractPathName(request , response){
    /*Request URL is being parsed using the parse method of the URL module which will return us the url
    which will contain pathname search and the host details as well */
    let requestURL = url.parse(request.url);
    /*Extracting the pathname of the URL which wiss give us only the fragment identifier string , for eg :
    http://localhost:/params?name=Manish , patname will only return us the /params*/
    let pathName = requestURL.pathname;
    return pathName;
}

/*
Function to be called when there is an incoming request to the server from client , we will be calling the 
extractPathName method and on the basis of the patname returned will decide which function to be called
*/
function router(request , response){
    let requestURLPathName = extractPathName(request , response);

    switch(requestURLPathName)
    {
    //If we get a request for /params path then we are going to call the paramsURLRouter method
    case "/params/:fName/:lName" :
        paramsURLRouter.paramsURLRouter(request , response);
        break;

路径名被检索为/ params / Name / LastName,因此它与任何情况都不匹配并且默认情况被调用。我希望将路径名称为/ params,并将片段标识符的其余部分作为请求的参数。如何确保在我们使用Express.js时将这些定义为请求参数。 请帮忙。提前谢谢。

0 个答案:

没有答案