How to run multiple express/Nodejs application in localhost?

血红的双手。 提交于 2019-12-10 11:44:30

问题


I am running my client(frondend) app express app in a port 3000 and another admin express app in 8080.

But when I am navigating any pages or refreshing any page in client express app, the session in admin app is lost and redirecting to login.

I am using Express-session npm for admin and frond end is just like a cms frondend, ie no sessions or anything complex. Can any body tell me why is this happening?


回答1:


Cookies for an app running on port 3000 are also sent to an app running on port 8080 (and vice versa).

My guess is that you didn't give each app a unique cookie name, so you might get interference between the session handling of both apps.

So, use a different cookie name for each:

app.use(session({
  name : 'frontend.sid', // and, say, 'admin.sid' for the admin app
  ...
}));



回答2:


Run in different browsers :

try one of them in different browser like one in chrome and other in mozilla. It is happening due to session clashing over there.

Changing the browser might be a good solution for you.

Want to run in same browser :

Just run one service in normal mode and run other service in incognito mode




回答3:


Based on the provided information I would suggest that you look over to the way you are initialising express-session. In case that the secure flag is set to true, it would expect a valid HTTPS certificate to be set for localhost, otherwise the session wont be created which can cause redirect problems.

From the docs:

secure: Specifies the boolean value for the Secure Set-Cookie attribute. When truthy, the Secure attribute is set, otherwise it is not. By default, the Secure attribute is not set. Please note that secure: true is a recommended option. However, it requires an https-enabled website, i.e., HTTPS is necessary for secure cookies. If secure is set, and you access your site over HTTP, the cookie will not be set.



来源:https://stackoverflow.com/questions/39888199/how-to-run-multiple-express-nodejs-application-in-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!