我正在Node js中构建REST api服务器。假设我有一个Api-http://localhost:3000/api/employee/:employeeId
。而且我想在不使用Express js之类的框架的情况下获取employeeId参数。如果可能的话,哪种方法更好?
答案 0 :(得分:0)
**如果您需要Url中的员工ID,则可以使用它-**
const express = require('express'); const router = express.Router();
router.get('http://localhost:3000/api/employee/:employeeId',getUserById);
getUserById(req,res){
console.log('---EmployeeId ----',req.params.employeeId);
}
答案 1 :(得分:-2)
这是一个通用的js函数,用于获取网址段
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];