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.