user2127480
user2127480

Reputation: 4731

How to write multi-line paths for routing?

Sometimes the routing path is too long so I want the path to display in multiple lines for readability.

I know the normally a multiple line string is written like this:

var str = 'hello \
           world \
           hi;

However, this doesn't work in express.js routing.

router.route('/:hello/ \
               :world/ \
               :hi').get(...);

But this works:

router.route('/:hello/:world/:hi').get(...);

Any ideas?

Upvotes: 2

Views: 318

Answers (2)

plalx
plalx

Reputation: 43728

Another way of doing it would be to use Array.prototype.join. It used to be faster than using the + operator, however it seems this have changed with modern browsers. Still, perhaps you prefer , over + for readability, but that's just a question of style at this point.

router.route([
    '/:hello',
    '/:world',
    '/:hi'
].join(''));

Upvotes: 0

hugomg
hugomg

Reputation: 69954

I often see people use string concatenation for this kind of thing

router.route(
    '/:hello'+
    '/:world'+
    '/:hi'
)

In fact, some JS compressors for client-side code even have special logic for concatenating these bbroken up strings into a big single-line string.

Upvotes: 2

Related Questions