2
급행에있는 경로를 동적으로 사용하는 방법 생각해보십시오. 예를 들어, 정규식 메서드를 사용하여 다른 파일에서 경로를 찾는 데 lodash를 사용하고 있습니다. 급행 노선에있는 동적 인 경로
const json = require('./routes.json')
const _ = require('lodash')
routes.use(function(req, res, next) {
let str = req.path
let path = str.split('/')[1]
// [Request] => /test/123
console.log(path)
// [Result] => test
let test = _.find(json.routes, function(item) {
return item.path.match(new RegExp('^/' + path + '*'))
})
console.log(test)
//{"path" : "/test/:id", "target" : "localhost:2018", "message" : "This is Test Response" },
routes.get(test.path, function(req, res) {
res.json("Done")
})
})
routes.js
, 난 그냥 경로를 중첩. 그러나 아무런 반응이 없습니다. 이것을 할 수있는 방법이 있습니까? 이 방법은 또한 필요한 경우 DB와 함께 사용하고 싶습니다. 어쨌든 고마워요