【问题标题】:How can I get the hexadecimal numbers from the base URL string如何从基本 URL 字符串中获取十六进制数字
【发布时间】:2022-01-16 17:30:27
【问题描述】:

我在 node js express js 中编写代码 但是在工作时,我想如果 hexadecimal 数字是从基本 URL 中获取的字符串,那么我的工作会更容易。

我的意思是:

baseUrl: '/api/v1/movies/61b6e1c5503b122ff9436b14/seasons'(从 req.baseUrl 获取)

我的基本 URL 字符串是:'/api/v1/movies/61b6e1c5503b122ff9436b14/seasons'

我只需要:61b6e1c5503b122ff9436b14

我目前正在使用 javaScript replace() 方法,但它对我来说似乎不是很有效。我特别想知道任何好的方法。

谢谢

【问题讨论】:

  • 如果您正在寻找动态路线,请检查问题的其他两个答案

标签: javascript node.js express mongoose


【解决方案1】:

Regexp 会很有用 unless you are looking for dynamic routes

这会得到代码

let baseUrl =  '/api/v1/movies/61b6e1c5503b122ff9436b14/seasons'
const hex = baseUrl.match(/movies\/(\w+)\/seasons/)[1]
console.log(hex.toUpperCase());

【讨论】:

    【解决方案2】:

    ExpressJS中可以使用req.params映射路由参数

    app.get('/api/v1/movies/:hashid/seasons', function (req, res) {
      console.log(req.params.hashid)
    })
    

    【讨论】:

      【解决方案3】:

      您要查找的内容称为dynamic routes,并且在快递中您可以这样做:

      const express = require('express');
      const app = express();
      
      app.get('/api/v1/movies/:hexid/seasons', (req,res) => {
      
         console.log(req.params.hexid);
      
      })
      

      您可以在他们的docs 中找到更多信息(搜索:bookId

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-18
        • 2013-01-26
        • 2019-07-27
        • 2018-11-03
        • 2012-05-04
        相关资源
        最近更新 更多