question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Separating Routes?

See original GitHub issue

The example in the README for the server renderer is basically a catch-all route: return (req, res, next) => {. What if you want to have multiple routes react differently? Do you have to check for the request url and a bunch of if/else statements? Or is there access to app somewhere that allows you to add other app.use statements for different routes (or other things, like adding middleware for example)?

Hopefully what I’m asking is clear enough to understand. I’m a node newb so pardon my ignorance.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
reintroducingcommented, May 24, 2019

good to know, thanks for your help. i don’t have an immediate need to do this right now, it was more of a theoretical question for when the need arises, so i’ll try it when that happens. i’ll close the issue for now, thank you.

1reaction
richardscarrottcommented, May 24, 2019

The router would be returned from your server renderer function, e.g.

export default function serverRenderer() {
    const router = Router();
    router.use(
    (req, res, next) => {
        res.status(200).send(`
            <!doctype html>
            <html>
            <head>
                <title>App</title>
            </head>
            <body>
                <div id="root">
                    ${renderToString(<App />)}
                </div>
                <script src="/client.js"></script>
            </body>
            </html>
        `)
    });
    return router;
}

However, for any routes unrelated to server side rendering such as a healthcheck (usually any code that doesn’t need to be compiled with webpack) you can just mount them in your main app before you mount the server renderer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to separate routes on Node.js and Express 4?
As far as separating routes from main file is concerned.. Server.js //include the routes file var routes = require('./routes/route'); var ...
Read more >
Separating logic from Express routes for easier testing
Separating logic from Express routes for easier testing. Have you ever been confused on how to structure your Express applications in a way...
Read more >
How to Create Separate Routes File in Node JS Express?
i explained simply about separate file for routes in express node.js. In this tutorial, i will give you two examples of how to...
Read more >
Express Tutorial Part 4: Routes and controllers - MDN Web Docs
Defining and using separate route modules. The code below provides a concrete example of how we can create a route module and then...
Read more >
Combining and Splitting Routes - Ride with GPS
Premium subscribers can combine multiple routes or split a long route into many using our simple split and combine tools. For advanced route...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found