First about the structure of the application. The app.js file contains the configuration of the main application and two sub-applications (admin, auth), it looks like this:

 //общие настройки let app = express(); // настройка app //настройки для админпанели let admin = express(); // настройка admin //настройки для под-приложения аутентификации let auth = express(); // настройка auth //Здесь подключаем роуты: require("router/root")({app:app,admin:admin,auth:auth}); //теперь под-приложение аутентификации будет доступно по адресу: localhost:3000/auth app.use("/auth", secure); //под-приложение админпанели вешаем на субдомен, должно быть доступно по адресу: admin.localhost:3000/ app.use(subdomain('admin', admin)); module.exports = app; 

Now about the routs (we connected this file to app.js like this: require("router/root")({app:app,admin:admin,auth:auth}) ):

 module.exports = (options) =>{ options.app.use('/', require('router/app/index')); options.app.use('/test', require('router/app/test')); options.admin.use('/', require('router/admin/index')); options.admin.use('/section', require('router/admin/section')); options.admin.use('/system', require('router/admin/system')); options.auth.use('/', require('router/auth/index')); options.auth.use('/logout', require('router/auth/logout')); }; 

Well, each connected route looks like this:

 const express = require("express"); const router = express.Router(); router.get("/", (req, res) =>{ //TODO actions... }); router.post("/my-url", (req, res) =>{ //TODO actions... }); module.exports = router; 

Now about the problems:

In order to organize the sub-domain admin.localhost I used this package: express-subdomain

But, for some reason, it doesn’t come to me, why when I admin.localhost:3000/ the sub-application for the admin panel does not work, but the main page is displayed (the main app application). It turns out that the string app.use(subdomain('admin', admin)); Does nothing at all. At the same time, the auth sub-application, as intended, works at localhost:3000/auth .

How do I get the admin sub-application to work at the address admin.localhost:3000/ ? Or if there is an option to use a different package to organize sub-domains, suggest.

UPDATE :

The problem was solved, I used another package for processing subdomains: link

From here there was a problem with cookies, you need to somehow do cross-domain authorization, cookies are written to the main localhost domain when we log in localhost/auth/doLogin/ , but when we switch to admin.localhost , we get 403, because this subdomain does not have cookies with user data and the session does not apply to it. How I use cookies:

 app.use(subdomain({base:'localhost',removeWWW:true })); app.use(session({ secret:'mysecret', store: new redisStore({client: redis }), key:'skey', resave: false, saveUninitialized: true, cookie: { path:"/", httpOnly:true, maxAge:null, domain:'.localhost' } })); 

How cookies are written:

 res.cookie('ID',id, { maxAge: 1000000, httpOnly: true }); res.cookie('PASS',pass, { maxAge: 1000000, httpOnly: true }); 

Tell me how to solve this issue?

  • I think you should prepare a minimal reproducible example . There is a feeling that because of the large amount of code that is not directly related to the problem, you are missing something important. - Dmitriy Simushev
  • @DmitriySimushev, I don’t think because This code can be cut a lot, it is big and there are some extra parts in it so that you can understand how the structure works. I think the point is specifically in the application of the subdomain , but I cannot understand it because I have not dealt with it earlier - sanu0074
  • In my experience, in 98% of cases, there is no error in the third-party library. There is an error somewhere in your code. And if so - it must be localized (with all the consequences). =) - Dmitriy Simushev

1 answer 1

It is necessary to change localhost to some other domain and the session will be extended to sub-domains.