Grouping Routes in Express
We can group our routes like this in Laravel:
Route::group("admin", ["middleware" => ["isAdmin"]], function () { Route::get("/", " AdminController@index "); Route::post("/post", ["middleware" => "csrf", "uses" => " AdminController@index "); }); Basically, all routes defined in the administration group are automatically named middleware and group isAdmin . For example, the post endpoint listens on admin/post not /post
Is there a way to do the same with Express? That would be great, because my Laravel routes were so clean, while my express routes are a bit messy / duplicated.
These are my .js routes to Express at the moment.
app.get("/admin", [passportConfig.isAuthenticated, passportConfig.isAdmin], AdminController.index); app.post("/admin", [passportConfig.isAuthenticated, passportConfig.isAdmin], AdminController.postIndex); Thanks.
You can use app.use() - https://expressjs.com/en/guide/using-middleware.html#middleware.application
app.use("/admin",[passportConfig.isAuthenticated, passportConfig.isAdmin],AdminController) // AdminController: var express = require('express'); var router = express.Router(); router.get('/', AdminController.index); // etc... module.exports = router