|
@@ -12,12 +12,14 @@ const database = new Sequelize({
|
|
|
const Attendee = database.define('attendee', {
|
|
|
name: Sequelize.STRING,
|
|
|
guest: Sequelize.BOOLEAN,
|
|
|
+ cat: Sequelize.STRING
|
|
|
});
|
|
|
|
|
|
-const whitelist = ['http://localhost:3001', 'http://christmas.logicp.ca']
|
|
|
+const whitelist = ['http://localhost:3001', 'http://localhost', 'http://127.0.0.1', 'http://christmas.logicp.ca']
|
|
|
|
|
|
const corsOptions = {
|
|
|
origin: function (origin, callback) {
|
|
|
+ console.log('request attempted from ', origin)
|
|
|
if (whitelist.indexOf(origin) !== -1) {
|
|
|
callback(null, true)
|
|
|
} else {
|
|
@@ -37,7 +39,7 @@ class App {
|
|
|
middleware() {
|
|
|
// this.express.use(logger('dev'));
|
|
|
this.express.use(bodyParser.json());
|
|
|
- this.express.use(bodyParser.urlencoded({ extended: false }));
|
|
|
+ this.express.use(bodyParser.urlencoded({ extended: true }));
|
|
|
// this.express.engine('html', require('ejs').renderFile);
|
|
|
}
|
|
|
// Configure API endpoints.
|
|
@@ -50,20 +52,37 @@ class App {
|
|
|
res.json({response: 'yo mamma'})
|
|
|
})
|
|
|
|
|
|
- this.express.post('/attendee', (req, res) => {
|
|
|
- console.log(req.body)
|
|
|
- // Attendee.findOrCreate({ name: 'Emmanuel', guest: true }).then(() =>
|
|
|
- Attendee.findOrCreate({
|
|
|
- where: {
|
|
|
- name: 'Emmanuel'
|
|
|
- }, defaults: {name: 'Emmanuel', guest: true}
|
|
|
- })
|
|
|
- .spread((param1, param2) => {
|
|
|
- console.log('Param 1')
|
|
|
- console.log(param1)
|
|
|
- console.log('Param 2')
|
|
|
- console.log(param2)
|
|
|
+ this.express.get('/list', cors(corsOptions), (req, res) => {
|
|
|
+ Attendee.findAll().then(attendees => {
|
|
|
+ res.json({attendees: attendees})
|
|
|
})
|
|
|
+ })
|
|
|
+
|
|
|
+ this.express.post('/attendee', cors(corsOptions), (req, res) => {
|
|
|
+ console.log(req.body)
|
|
|
+ if ('name' in req.body) {
|
|
|
+ Attendee.findOrCreate({
|
|
|
+ where: {
|
|
|
+ name: req.body.name
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .spread( attendee => {
|
|
|
+ console.log(attendee)
|
|
|
+ attendee.guest = req.body.significantOther
|
|
|
+ attendee.cat = req.body.cat
|
|
|
+ Attendee.update({
|
|
|
+ name: req.body.name,
|
|
|
+ cat: req.body.cat,
|
|
|
+ guest: req.body.significantOther
|
|
|
+ }, {
|
|
|
+ where: {
|
|
|
+ id: attendee.id
|
|
|
+ }
|
|
|
+ }).then( attendee => {
|
|
|
+ console.log('Successfully updated attendee:', attendee)
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
res.json({response: 'yeah we got you'})
|
|
|
})
|
|
|
}
|
|
@@ -72,6 +91,20 @@ class App {
|
|
|
const app = new App().express
|
|
|
|
|
|
app.listen(3002)
|
|
|
+// app.options('/attendee', function (req, res) {
|
|
|
+// res.setHeader("Access-Control-Allow-Origin", whitelist.join('|'));
|
|
|
+// res.setHeader('Access-Control-Allow-Methods', '*');
|
|
|
+// res.setHeader("Access-Control-Allow-Headers", "*");
|
|
|
+// res.end();
|
|
|
+// });
|
|
|
+
|
|
|
+app.all('*', function(req, res, next) {
|
|
|
+ if (whitelist.indexOf(req.headers.origin) >= 0){
|
|
|
+ res.header("Access-Control-Allow-Origin", req.headers.origin);
|
|
|
+ }
|
|
|
+ res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
|
+ next();
|
|
|
+})
|
|
|
|
|
|
database.sync().then(() => {
|
|
|
console.log(`Listening on port ${3002}`);
|