Next-routes: Improve Documentation (How to setup)

Created on 28 May 2018  路  4Comments  路  Source: fridays/next-routes

In general it's pretty well documented but the last steps seem to be missing to get it up and running.

Under "How to use" you describe the routes.js file setup.

const routes = module.exports = require('next-routes')()

routes
.add('about')
.add('blog', '/blog/:slug')
.add('user', '/user/:id', 'profile')
.add('/:noname/:lang(en|es)/:wow+', 'complex')
.add({name: 'beta', pattern: '/v3', page: 'v3'})

But you don't explain how and what to export.

In the section "On the server" there is routes imported/required but we didn't export anything in routes.js.

...
const routes = require('./routes')
...

If I do every step as explained and export routes from routes.js as:

// routes.js
...
module.exports = routes;

I still get an 404 after refresh and my node server (express) doesn't find my defined routes.

Also you explain in "On the client", Link should also be used from routes.js but we didn't export that either.

Maybe an example would help as well.

Most helpful comment

This line const routes = module.exports = require('next-routes')(); is an export. You can see module.exports getting assigned right there.

All 4 comments

Ok, I got it:

// routes.js
const routes = module.exports = require('next-routes')(); 

routes 
    .add('p', '/p/:slug', 'product') 
    .add('c', '/c/:slug', 'content') 
    .add('service', '/service/:slug') 
    .add('legal', '/legal/:slug'); 

module.exports.Link = routes.Link; 
module.exports.Router = routes.Router; 
module.exports = routes; 

and I had to add/remove

// server.js
...
- const handle = app.getRequestHandler(); 
+ const handler = routes.getRequestHandler(app); 
...
- server.get('*', (req, res) => { 
-     return handle(req, res) 
- }); 
server 
+    .use(handler) 
    .listen(3000, (err) => { 
        if(err) throw err; 
        console.log('> Ready on http://localhost:3000'); 
     }); 

This line const routes = module.exports = require('next-routes')(); is an export. You can see module.exports getting assigned right there.

I'm having issues with this too .. external links to the app throw error 404. Don't know what I'm doing wrong

@mherodev that's right!

@chrisehlee If your getting 404 please check if your starting the app by running node server.js (instead of next start)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidnguyen179 picture davidnguyen179  路  6Comments

jesstelford picture jesstelford  路  3Comments

jchiellini picture jchiellini  路  4Comments

mocheng picture mocheng  路  5Comments

bliitzkrieg picture bliitzkrieg  路  4Comments