Express Subdomain and Route Returning Cors Error

I have a angular and nodejs app that worked perfectly fine. The application is an angular application with a nodejs backend hosted on heroku. I recently introduces subdomains to the nodejs server using express-subdomain.

I created a separate subdomain for swagger as well as the api and also host the frontend on the same server. I also kept the previous route based routing that was in place before. So now I am able to access swagger with the swagger subdomain as well as /swagger route.

Although the subdomain and routing is working, I am running into a cors issue with the api’s. When debugging, some api’s are returning cors error and the only error I am seeing in the chrome debug console is Error ok.

I created an express app for the main app, swagger and the api. Can that be the issue? Also is my approach correct or is there a possible better alternative to this?

my server.js is:

require('rootpath')();
const express = require('express');
const allRoutes = express();
const swagger = express();
var exec = require('exec');
var app = express();
const cors = require('cors');
const bodyParser = require('body-parser');
var path = require('path');
const jwt = require('./backend/_helpers/jwt');
const errorHandler = require('./backend/_helpers/error-handler');
var http = require('http');
const swaggerUi = require('swagger-ui-express')
const swaggerFile = require('./swagger-output.json');
const config = require('config');
const { OpenAI } = require('openai');
const subdomain = require('express-subdomain');

global.openai = new OpenAI({ apiKey: process.env.openAiKey ? process.env.openAiKey : config.get('openAiKey') })

var fs = require('fs');

var options = {

  key: fs.readFileSync('./src/assets/go_com_key.txt'),

  cert: fs.readFileSync('./src/assets/go.com.crt'),

  ca: fs.readFileSync ('./src/assets/go.com.ca-bundle')

};

//apiRoutes.use(enforce.HTTPS());
app.use(express.static(path.join(__dirname, '/dist/app'), { dotfiles: 'allow' }));

allRoutes.use(bodyParser.json({limit: '50mb'}));
allRoutes.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
allRoutes.use(cors());

// use JWT auth to secure the api
if (process.env.NODE_ENV !== "production" || process.env.ENV === "dev") {
    swagger.use('/', swaggerUi.serve, swaggerUi.setup(swaggerFile, { swaggerOptions: { persistAuthorization: true }}));
    app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerFile, { swaggerOptions: { persistAuthorization: true }}));
}

allRoutes.use('*', jwt());
// allRoutes.use(subdomain('api',  jwt()));
// api routes
allRoutes.use('/api/users', require('./backend/users/users.controller'));
allRoutes.use('/api/textMessages', require('./backend/textMessagesServices/textMessage.controller'));
allRoutes.use('/api/reports', require('./backend/report/report.controller'));
allRoutes.use('/api/qrDevices', require('./backend/qrDevices/QRDevice.controller'));
allRoutes.use('/api/orgSettings', require('./backend/orgSettings/orgSettings.controller'));

// square API's
allRoutes.use('squareApi', require('./backend/Partner-Integrations/squareApi/squareApi.controller'));
allRoutes.use('squarePay', require('./backend/Partner-Integrations/squarePayment/squarePayment.controller'));

//uber Api's
allRoutes.use('uberApi', require('./backend/Partner-Integrations/uberApi/uberApi.controller'));

// global error handler
// start server
app.use(subdomain('swagger', swagger));
app.use('/swagger', swagger);
app.use('/api', allRoutes);
app.use('/webhooks', require('./backend/webhooks/webhooks.controller'));
app.get('*', function(req,res) {
    console.log('req ', req);
    console.log('subdomain ', req.subdomains);
    if (!req.url.includes('swagger') && !req.subdomains.includes('swagger')) {  //&& !req.url.includes('webhooks')
        res.sendFile(path.join(__dirname+'/dist/app/index.html'));
    }
});
app.use("/", allRoutes);

allRoutes.use(errorHandler);
app.use(errorHandler);

const port = process.env.PORT ? (process.env.PORT) : 4000;
var server = http.createServer(options, app);

server.listen(port, () => {
    console.log("server starting on port : " + port)
});
// Socket Layer over Http Server
const socket = require('socket.io')(server, {
    cors: {
        origin: config.get('webSocketUrl'),
        methods: ["GET", "POST"]
    }
});
// On every Client Connection
socket.on('connection', socket => {
    console.log('Socket: client connected');
});

app.set('socketio', socket);
function execCallback(err, stdout, stderr) {
    if (stdout) {
        console.log(stdout)
    }
    if (stderr) {
        console.log(stderr)
    }
}

None of the other subdomain packages worked.

Thanks in advance for the help.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật