core icon indicating copy to clipboard operation
core copied to clipboard

Newly created routes are not registered by the server unless its restarted.

Open noncototient opened this issue 10 years ago • 9 comments

Not sure if it supposed to be this way, but here is what I came across:

Adding a new route in routes.js files while the server is running like so:

'use strict'
const Route = use('Route')

Route.on('/contact').render('pages.contact')

/* New route (while the server is still running) */
Route.on('/contact').render('pages.contact')

And then going to the following route results in an error:

Route not found /contact

With the following log:

HttpException: Route not found /contact
    at Server._callRouteAction (/Users/user/Sites/adonis/node_modules/adonis-framework/src/Server/index.js:71:13)
    at finalHandler (/Users/user/Sites/adonis/node_modules/adonis-framework/src/Server/index.js:246:18)
    at next (native)
    at onFulfilled (/Users/user/Sites/adonis/node_modules/co/index.js:65:19)
    at /Users/user/Sites/adonis/node_modules/co/index.js:54:5
    at co (/Users/user/Sites/adonis/node_modules/co/index.js:50:10)
    at toPromise (/Users/user/Sites/adonis/node_modules/co/index.js:118:63)
    at next (/Users/user/Sites/adonis/node_modules/co/index.js:99:29)
    at onFulfilled (/Users/user/Sites/adonis/node_modules/co/index.js:69:7)

However, restarting the server via npm start resolves the issues.

noncototient avatar Aug 20 '16 10:08 noncototient

Actually, the server needs to always be restarted for some changes to take effect. Things like using faker to generate fake data in MySQL and send it to the view from the controller <- will not log or show any errors, just won't display any data until the server is restarted.

Am I missing something?

noncototient avatar Aug 20 '16 11:08 noncototient

You cannot add routes while the server is running, they are evaluated only once. Logically it does not even make sense to add routes in runtime.

thetutlage avatar Aug 20 '16 11:08 thetutlage

Why is it illogical? Laravel does it, I'm sure they know what they are doing.

noncototient avatar Aug 20 '16 11:08 noncototient

Node is not PHP. If you will try to understand how Node.js process run vs how PHP runs, you will find the answer

thetutlage avatar Aug 20 '16 11:08 thetutlage

Obviously Node is not PHP.

How about this: https://github.com/remy/nodemon?

noncototient avatar Aug 20 '16 11:08 noncototient

Node.js has a long running process which PHP does not have. On each HTTP request PHP will re-run from start to end.

Node.js loads everything into memory and run the HTTP server as a single long running process. Which means objects which are supposed to be evaluated once are not re-evaluated.

Nodemon does nothing but restarts the server on file change.

Creating dynamic routes from anywhere in the application is not a good practice, since you have to scan the entire codebase to understand from where all routes are being created.

It is better to improve the application design, instead of finding ways to ditch the code.

thetutlage avatar Aug 20 '16 11:08 thetutlage

I see, thanks

noncototient avatar Aug 20 '16 11:08 noncototient

@thetutlage I want to write a gateway to delivery my request to many microservice, so do you have any solution for reload routing? Maybe call manually in code without restart app.

Thanks

tavsta avatar Apr 28 '18 08:04 tavsta

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Mar 11 '20 06:03 lock[bot]