如何编写用于路由的多行path?
有时路由path太长,所以我想要path显示在多行为可读性。
我知道通常一个多行string是这样写的:
var str = 'hello \ world \ hi;
但是,这在express.js路由中不起作用。
router.route('/:hello/ \ :world/ \ :hi').get(...);
但是这个工作:
router.route('/:hello/:world/:hi').get(...);
有任何想法吗?
我经常看到人们为这种事情使用string连接
router.route( '/:hello'+ '/:world'+ '/:hi' )
实际上,一些用于客户端代码的JS压缩程序甚至还有一些特殊的逻辑,用于将这些打包的string连接成一个大的单行string。
另一种方法是使用Array.prototype.join
。 它曾经比使用+
运算符更快,但是现代浏览器似乎已经改变了。 不过,也许你更喜欢,
为了可读性+
,但这只是一个风格的问题在这一点上。
router.route([ '/:hello', '/:world', '/:hi' ].join(''));