I am doing codes in node js express js But while working, I thought that if the hexadecimal number was taken from the base URL string, then my work would be easier.
I mean:
baseUrl: '/api/v1/movies/61b6e1c5503b122ff9436b14/seasons' (Get from req.baseUrl)
My base URL string is: '/api/v1/movies/61b6e1c5503b122ff9436b14/seasons'
I need just: 61b6e1c5503b122ff9436b14
I am currently using the javaScript replace() method but it does not seem to be very effective to me. I am especially interested to know any good method.
Thanks
CodePudding user response:
Regexp would be useful unless you are looking for dynamic routes
This would get the code
let baseUrl = '/api/v1/movies/61b6e1c5503b122ff9436b14/seasons'
const hex = baseUrl.match(/movies\/(\w )\/seasons/)[1]
console.log(hex.toUpperCase());
CodePudding user response:
What you're looking for are called dynamic routes
and in express you can do like this:
const express = require('express');
const app = express();
app.get('/api/v1/movies/:hexid/seasons', (req,res) => {
console.log(req.params.hexid);
})
You can find more info in their docs ( search for :bookId
)
CodePudding user response:
You can use req.params
to map the route parameters in ExpressJS
app.get('/api/v1/movies/:hashid/seasons', function (req, res) {
console.log(req.params.hashid)
})