change: enable CORS preflight on /api/:type

This commit is contained in:
josephangelobravo@gmail.com 2023-03-28 15:05:06 -07:00
parent adba73d60c
commit 87e684543e

View file

@ -27,6 +27,7 @@ const branch = getCurrentBranch();
const app = express(); const app = express();
app.disable('x-powered-by'); app.disable('x-powered-by');
app.use('/api/:type', cors())
if (fs.existsSync('./.env') && process.env.selfURL && process.env.streamSalt && process.env.port) { if (fs.existsSync('./.env') && process.env.selfURL && process.env.streamSalt && process.env.port) {
const apiLimiter = rateLimit({ const apiLimiter = rateLimit({
@ -80,7 +81,7 @@ if (fs.existsSync('./.env') && process.env.selfURL && process.env.streamSalt &&
} }
})); }));
app.post('/api/json', cors({ origin: process.env.selfURL, optionsSuccessStatus: 200 }), async (req, res) => { app.post('/api/json', async (req, res) => {
try { try {
let ip = sha256(req.header('x-forwarded-for') ? req.header('x-forwarded-for') : req.ip.replace('::ffff:', ''), process.env.streamSalt); let ip = sha256(req.header('x-forwarded-for') ? req.header('x-forwarded-for') : req.ip.replace('::ffff:', ''), process.env.streamSalt);
let lang = languageCode(req); let lang = languageCode(req);
@ -104,7 +105,7 @@ if (fs.existsSync('./.env') && process.env.selfURL && process.env.streamSalt &&
} }
}); });
app.get('/api/:type', cors({ origin: process.env.selfURL, optionsSuccessStatus: 200 }), (req, res) => { app.get('/api/:type', (req, res) => {
try { try {
let ip = sha256(req.header('x-forwarded-for') ? req.header('x-forwarded-for') : req.ip.replace('::ffff:', ''), process.env.streamSalt); let ip = sha256(req.header('x-forwarded-for') ? req.header('x-forwarded-for') : req.ip.replace('::ffff:', ''), process.env.streamSalt);
switch (req.params.type) { switch (req.params.type) {