Commit 2a9f572e authored by Dennis Willers's avatar Dennis Willers 🏀

Set Header 4 Cors

parent dd4bf68c
Pipeline #324 passed with stages
in 3 minutes
const express = require('express'); const express = require('express');
const io = require("socket.io-client"); const io = require("socket.io-client");
//const socket = io('ws://tabu-server.willers.digital'); const socket = io('ws://tabu-server.willers.digital');
const socket = io('ws://localhost:80'); //const socket = io('ws://localhost:80');
function createRouter(db) { function createRouter(db) {
const router = express.Router(); const router = express.Router();
...@@ -15,7 +15,7 @@ function createRouter(db) { ...@@ -15,7 +15,7 @@ function createRouter(db) {
}); });
const isTrue = function(req, res) { const isTrue = function(req, res) {
res.status(200).json({status: 'true'}); res.header('Access-Control-Allow-Origin', "*").status(200).json({status: 'true'});
}; };
const isSession = function isSession(req, res, next) { const isSession = function isSession(req, res, next) {
...@@ -28,18 +28,18 @@ function createRouter(db) { ...@@ -28,18 +28,18 @@ function createRouter(db) {
(error, results) => { (error, results) => {
if (error) { if (error) {
console.log(error); console.log(error);
return res.status(500).json({status: 'error'}); return res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error'});
} else { } else {
if (results.length > 0) { if (results.length > 0) {
next(); next();
} else { } else {
return res.status(200).json({status: 'false'}); return res.header('Access-Control-Allow-Origin', "*").status(200).json({status: 'false'});
} }
} }
} }
); );
} else { } else {
return res.status(500).json({status: 'error'}); return res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error'});
} }
}; };
...@@ -53,9 +53,9 @@ function createRouter(db) { ...@@ -53,9 +53,9 @@ function createRouter(db) {
(error, results) => { (error, results) => {
if (error) { if (error) {
console.log(error); console.log(error);
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else { } else {
res.status(200).json( res.header('Access-Control-Allow-Origin', "*").status(200).json(
{ {
status: 'true', status: 'true',
sessionID: results[0].SessionID, sessionID: results[0].SessionID,
...@@ -70,7 +70,7 @@ function createRouter(db) { ...@@ -70,7 +70,7 @@ function createRouter(db) {
} }
); );
} else { } else {
res.status(500).json({status: 'error'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error'});
} }
}; };
...@@ -84,7 +84,7 @@ function createRouter(db) { ...@@ -84,7 +84,7 @@ function createRouter(db) {
error => { error => {
if (error) { if (error) {
console.error(error); console.error(error);
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else { } else {
//Init new Gamestatus //Init new Gamestatus
var redTurn = Math.random(); var redTurn = Math.random();
...@@ -96,9 +96,9 @@ function createRouter(db) { ...@@ -96,9 +96,9 @@ function createRouter(db) {
error => { error => {
if (error) { if (error) {
console.error(error); console.error(error);
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else { } else {
res.status(200).json({status: 'true'}); res.header('Access-Control-Allow-Origin', "*").status(200).json({status: 'true'});
} }
} }
); );
...@@ -106,7 +106,7 @@ function createRouter(db) { ...@@ -106,7 +106,7 @@ function createRouter(db) {
} }
); );
} else { } else {
res.status(500).json({status: 'error'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error'});
} }
}; };
...@@ -118,9 +118,9 @@ function createRouter(db) { ...@@ -118,9 +118,9 @@ function createRouter(db) {
(error, results) => { (error, results) => {
if (error) { if (error) {
console.log(error); console.log(error);
res.status(500).json({status: 'error'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error'});
} else { } else {
res.status(200).json(results); res.header('Access-Control-Allow-Origin', "*").status(200).json(results);
} }
} }
); );
...@@ -144,7 +144,7 @@ function createRouter(db) { ...@@ -144,7 +144,7 @@ function createRouter(db) {
(error, results) => { (error, results) => {
if (error) { if (error) {
console.log(error); console.log(error);
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else { } else {
if (results.length > 0) { if (results.length > 0) {
let random = Math.random() * (results.length-1); let random = Math.random() * (results.length-1);
...@@ -156,9 +156,9 @@ function createRouter(db) { ...@@ -156,9 +156,9 @@ function createRouter(db) {
sql, sql,
(error) => { (error) => {
if (error) { if (error) {
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else { } else {
res.status(200).json( res.header('Access-Control-Allow-Origin', "*").status(200).json(
{ {
status: 'true', status: 'true',
cardID: card.CardID, cardID: card.CardID,
...@@ -175,12 +175,12 @@ function createRouter(db) { ...@@ -175,12 +175,12 @@ function createRouter(db) {
} }
); );
} else { } else {
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} }
} }
}); });
} else { } else {
res.status(500).json({status: 'error'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error'});
} }
}; };
...@@ -194,9 +194,9 @@ function createRouter(db) { ...@@ -194,9 +194,9 @@ function createRouter(db) {
(error) => { (error) => {
if (error) { if (error) {
console.log(error); console.log(error);
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else { } else {
res.status(200).json({status: 'true'}); res.header('Access-Control-Allow-Origin', "*").status(200).json({status: 'true'});
socket.emit('newRound', { socket.emit('newRound', {
sessionName: spielname, sessionName: spielname,
isOpen: true isOpen: true
...@@ -206,7 +206,7 @@ function createRouter(db) { ...@@ -206,7 +206,7 @@ function createRouter(db) {
} }
); );
} else { } else {
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} }
}; };
...@@ -221,18 +221,18 @@ function createRouter(db) { ...@@ -221,18 +221,18 @@ function createRouter(db) {
if (error) { if (error) {
console.log(error); console.log(error);
return return
//res.status(500).json({status: 'false'}); //res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else { } else {
socket.emit('endRound', { socket.emit('endRound', {
isOpen: true, isOpen: true,
sessionName: spielname, sessionName: spielname,
}); });
//res.status(200).json({status: 'true'}); //res.header('Access-Control-Allow-Origin', "*").status(200).json({status: 'true'});
} }
} }
); );
} else { } else {
//res.status(500).json({status: 'false'}); //res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} }
}; };
...@@ -246,14 +246,14 @@ function createRouter(db) { ...@@ -246,14 +246,14 @@ function createRouter(db) {
(error) => { (error) => {
if (error) { if (error) {
console.log(error); console.log(error);
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else { } else {
res.status(200).json({status: 'true'}); res.header('Access-Control-Allow-Origin', "*").status(200).json({status: 'true'});
} }
} }
); );
} else { } else {
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} }
}; };
...@@ -270,9 +270,9 @@ function createRouter(db) { ...@@ -270,9 +270,9 @@ function createRouter(db) {
(error) => { (error) => {
if (error) { if (error) {
console.log(error); console.log(error);
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else { } else {
res.status(200).json( res.header('Access-Control-Allow-Origin', "*").status(200).json(
{ {
status: 'true', status: 'true',
red: JSON.parse(req.body.red)+1, red: JSON.parse(req.body.red)+1,
...@@ -290,9 +290,9 @@ function createRouter(db) { ...@@ -290,9 +290,9 @@ function createRouter(db) {
(error) => { (error) => {
if (error) { if (error) {
console.log(error); console.log(error);
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else { } else {
res.status(200).json( res.header('Access-Control-Allow-Origin', "*").status(200).json(
{ {
status: 'true', status: 'true',
red: req.body.red, red: req.body.red,
...@@ -304,7 +304,7 @@ function createRouter(db) { ...@@ -304,7 +304,7 @@ function createRouter(db) {
); );
} }
} else { } else {
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} }
}; };
...@@ -321,9 +321,9 @@ function createRouter(db) { ...@@ -321,9 +321,9 @@ function createRouter(db) {
(error) => { (error) => {
if (error) { if (error) {
console.log(error); console.log(error);
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else { } else {
res.status(200).json( res.header('Access-Control-Allow-Origin', "*").status(200).json(
{ {
status: 'true', status: 'true',
red: JSON.parse(req.body.red)-1, red: JSON.parse(req.body.red)-1,
...@@ -341,9 +341,9 @@ function createRouter(db) { ...@@ -341,9 +341,9 @@ function createRouter(db) {
(error) => { (error) => {
if (error) { if (error) {
console.log(error); console.log(error);
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} else { } else {
res.status(200).json( res.header('Access-Control-Allow-Origin', "*").status(200).json(
{ {
status: 'true', status: 'true',
red: req.body.red, red: req.body.red,
...@@ -355,7 +355,7 @@ function createRouter(db) { ...@@ -355,7 +355,7 @@ function createRouter(db) {
); );
} }
} else { } else {
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} }
}; };
...@@ -369,10 +369,10 @@ function createRouter(db) { ...@@ -369,10 +369,10 @@ function createRouter(db) {
(error, results) => { (error, results) => {
if (error) { if (error) {
console.log(error); console.log(error);
res.status(500).json({status: 'error'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'error'});
} else { } else {
if (results.length > 0) { if (results.length > 0) {
res.status(200).json({ res.header('Access-Control-Allow-Origin', "*").status(200).json({
cardID: results[0].CardID, cardID: results[0].CardID,
solution: results[0].Solution, solution: results[0].Solution,
tabu1: results[0].Tabu1, tabu1: results[0].Tabu1,
...@@ -382,7 +382,7 @@ function createRouter(db) { ...@@ -382,7 +382,7 @@ function createRouter(db) {
tabu5: results[0].Tabu5 tabu5: results[0].Tabu5
}); });
} else { } else {
res.status(500).json({status: 'false'}); res.header('Access-Control-Allow-Origin', "*").status(500).json({status: 'false'});
} }
} }
} }
...@@ -426,7 +426,6 @@ function createRouter(db) { ...@@ -426,7 +426,6 @@ function createRouter(db) {
router.post('/getCard', [getCard]); router.post('/getCard', [getCard]);
return router; return router;
} }
module.exports = createRouter; module.exports = createRouter;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment