Browse Source

added Cors

logicp 6 years ago
parent
commit
ca015c5fb2
1 changed files with 15 additions and 2 deletions
  1. 15 2
      src/server/index.js

+ 15 - 2
src/server/index.js

@@ -1,6 +1,7 @@
 const Sequelize = require('sequelize');
 const express = require("express");
 const bodyParser = require("body-parser");
+const cors = require('cors')
 require('dotenv').config({ path: '.env.local' });
 
 const database = new Sequelize({
@@ -13,6 +14,18 @@ const Attendee = database.define('attendee', {
   guest: Sequelize.BOOLEAN,
 });
 
+const whitelist = ['http://localhost:3001', 'http://christmas.logicp.ca']
+
+const corsOptions = {
+  origin: function (origin, callback) {
+    if (whitelist.indexOf(origin) !== -1) {
+      callback(null, true)
+    } else {
+      callback(new Error('Not allowed by CORS'))
+    }
+  }
+}
+
 class App {
     //Run configuration methods on the Express instance.
     constructor() {
@@ -32,7 +45,7 @@ class App {
         /* This is just to get up and running, and to make sure what we've got is
          * working so far. This function will change when we start to add more
          * API endpoints */
-        this.express.get('/test', (req, res) => {
+        this.express.get('/test', cors(corsOptions), (req, res) => {
           console.log(req)
           res.json({response: 'yo mamma'})
         })
@@ -57,8 +70,8 @@ class App {
 }
 
 const app = new App().express
-app.listen(3002)
 
+app.listen(3002)
 
 database.sync().then(() => {
     console.log(`Listening on port ${3002}`);