在 Node JS 中重用变量
Resuse a variable in NodeJS
我正在尝试使用(记录)函数的 return 对象而不对多个变量进行贴标
如何根据 nodeJS
/Javascript
最佳实践做到最好。
我是否必须多次声明这个变量,因为我通过引用知道它。
var configuredRoute;
configuredRoute = router.route('/api/v1/people')
.get(PersonController.all)
.post(PersonController.add)
logger.configured(configuredRoute.methods,configuredRoute.path)
configuredRoute = router.route('/api/v1/people/:id')
.get(PersonController.getOne)
.put(PersonController.update)
.delete(PersonController.remove)
logger.configured(configuredRoute.methods,configuredRoute.path)
configuredRoute = router.route('/api/v1/supervisor')
.get(auth.isAuthenticated(), PersonController.allSupervisors)
logger.configured(configuredRoute.methods,configuredRoute.path)
configuredRoute = router.route('/api/v1/people/status/:phone')
.get(PersonController.isRegistered)
logger.configured(configuredRoute.methods,configuredRoute.path)
我想你问的是如何在不声明更多变量的情况下对配置每条路由的结果调用 logger.configured
。一种解决方案是使用匿名函数来表示路由配置:
//assuming logger and router are declared in this scope
function configureRoute(route) {
const {methods, path} = route(router)
logger.configured(methods, path)
}
const routes = [
router =>
router.route('/api/v1/people')
.get(PersonController.all)
.post(PersonController.add),
router =>
router.route('/api/v1/people/:id')
.get(PersonController.getOne)
.put(PersonController.update)
.delete(PersonController.remove),
router =>
router.route('/api/v1/supervisor')
.get(auth.isAuthenticated(), PersonController.allSupervisors),
router =>
router.route('/api/v1/people/status/:phone')
.get(PersonController.isRegistered)
]
for (const route of routes) {
configureRoute(route)
}
我正在尝试使用(记录)函数的 return 对象而不对多个变量进行贴标
如何根据 nodeJS
/Javascript
最佳实践做到最好。
我是否必须多次声明这个变量,因为我通过引用知道它。
var configuredRoute;
configuredRoute = router.route('/api/v1/people')
.get(PersonController.all)
.post(PersonController.add)
logger.configured(configuredRoute.methods,configuredRoute.path)
configuredRoute = router.route('/api/v1/people/:id')
.get(PersonController.getOne)
.put(PersonController.update)
.delete(PersonController.remove)
logger.configured(configuredRoute.methods,configuredRoute.path)
configuredRoute = router.route('/api/v1/supervisor')
.get(auth.isAuthenticated(), PersonController.allSupervisors)
logger.configured(configuredRoute.methods,configuredRoute.path)
configuredRoute = router.route('/api/v1/people/status/:phone')
.get(PersonController.isRegistered)
logger.configured(configuredRoute.methods,configuredRoute.path)
我想你问的是如何在不声明更多变量的情况下对配置每条路由的结果调用 logger.configured
。一种解决方案是使用匿名函数来表示路由配置:
//assuming logger and router are declared in this scope
function configureRoute(route) {
const {methods, path} = route(router)
logger.configured(methods, path)
}
const routes = [
router =>
router.route('/api/v1/people')
.get(PersonController.all)
.post(PersonController.add),
router =>
router.route('/api/v1/people/:id')
.get(PersonController.getOne)
.put(PersonController.update)
.delete(PersonController.remove),
router =>
router.route('/api/v1/supervisor')
.get(auth.isAuthenticated(), PersonController.allSupervisors),
router =>
router.route('/api/v1/people/status/:phone')
.get(PersonController.isRegistered)
]
for (const route of routes) {
configureRoute(route)
}