I am building a Node Js application using Passport & Passport-SAML Packages for incorporating SAML Login. I have a front-end React app that need to be served after the user successfully authenticates via SAML Login. Using 'npm run build' command I was able to build the front-end React app and the same is being served as a static resource using express.static() middleware inside app.use() function. While running the code in my local & dev environments, I am not getting any error, when this is being moved to the elastic beanstalk production environment on AWS, I am getting this error:-
Stack Error:-
RangeError: Maximum call stack size exceeded
at next (/usr/src/app/server2/node_modules/express/lib/router/index.js:178:16)
at Layer.handle_error (/usr/src/app/server2/node_modules/express/lib/router/layer.js:67:12)
at trim_prefix (/usr/src/app/server2/node_modules/express/lib/router/index.js:310:13)
at /usr/src/app/server2/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/usr/src/app/server2/node_modules/express/lib/router/index.js:330:12)
at next (/usr/src/app/server2/node_modules/express/lib/router/index.js:271:10)
at Layer.handle_error (/usr/src/app/server2/node_modules/express/lib/router/layer.js:67:12)
at trim_prefix (/usr/src/app/server2/node_modules/express/lib/router/index.js:310:13)
at /usr/src/app/server2/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/usr/src/app/server2/node_modules/express/lib/router/index.js:330:12)
Coming to the details of the
I have gone through the issue and found out the cause for the issue/exception is normally caused by too many recursive calls. I have checked my code base and I didn't noticed any recursive call as such. There are only limited routes on the app.js file. Attaching the code snippets below:-
Code Snippets:-
`/**
* Home Page/Route of the application where it loads the SAML login page if the user is not Authenticated,
* loads the react app as a static resource if the user is Authenticated.
*
*/
app.get('/', function (req, res) {
app.use(express.static(path.join(__dirname, '..', 'build'))); // Binding the static resources from build folder using app.use() function
//'Checking whether the user is authenticated'
if (req.isAuthenticated()) {
res.sendFile(path.join(__dirname, '..', 'build', "index.html")); // Loading the react app from the build folder
} else {
res.writeHead(302, {
Location: "/login",
});
res.end();
}
});
/**
* Login endpoint, where passport will authenticate user using saml strategy configured.
*
*/
app.get('/login',
passport.authenticate(config.passport.strategy,
{
successRedirect: '/',
failureRedirect: '/login'
})
);
/**
* Endpoint to handle the post callback assertion by the SAML.
*/
app.post(config.passport.saml.path,
passport.authenticate(config.passport.strategy,
{
failureRedirect: '/',
failureFlash: true
}),
function (req, res) {
res.redirect('/');
}
);
`
Observations/Notes:-
- I am loading the react app as a static resource with the help of express.static() middleware using app.use() function inside a app.get('/') endpoint. Ideally the correct location to bind the static middleware is outside the endpoints and usually after the packages we import.
Reference:- Can I use app.use inside app.get with different paths
I tried to move the static middleware outside of the get ('/') endpoint, while loading the '/' endpoint rather than loading the SAML login page, it is directly opening the react app(static resources) as the client side routing will also have a '/' endpoint configured in the react app.
Though the app is being deployed onto a elastic beanstalk, there is a docker version difference I have noticed in both the environments(dev & prod), prod is using a retired version of docker.
Currently, I am working on adjusting the routes to load the static resources in the app. Also, the above link says "It's incorrect to put app.use inside app.get because middleware should be defined once, not on every request.". Relying on the article, is it a correct approach to make changes in the code base?
Please help me with any leads to overcome the issue. Thanks.