-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
81 lines (70 loc) · 2.23 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const express = require('express');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpack = require('webpack');
const webpackConfig = require('./webpack.config.js');
const app = express();
const db = require('./db/index.js')
const apiHelp = require('./APIhelper.js');
const bodyParser = require('body-parser');
const compiler = webpack(webpackConfig);
app.use(express.static(__dirname + '/www'));
app.use(bodyParser.json());
app.use(webpackDevMiddleware(compiler, {
hot: true,
filename: 'bundle.js',
publicPath: '/',
stats: {
colors: true,
},
historyApiFallback: true,
}));
app.post('/helper', (req, res) => {
apiHelp.couponHelper(req.body.postal, (data) => {
for(var i = 0; i < data.deals.length; i++) {
var eachDeal = data.deals[i]
db.Coupons.findOrCreate({where: {
imgUrl: eachDeal.deal.image_url,
title: eachDeal.deal.title,
price: JSON.stringify(eachDeal.deal.price),
discount: JSON.stringify(eachDeal.deal.discount_percentage),
merchant: eachDeal.deal.merchant.name,
url: eachDeal.deal.url,
pureUrl: eachDeal.deal.untracked_url
}
})
.spread((Teams, created) => {
console.log(Teams.get({
plain: true
}))
})
}
res.status(200).send('done!')
})
})
app.get('/arrayCoupons', (req, res) => {
db.Coupons.findAll({where: {saved: 'null'}, limit: 40}).then((data) =>{
res.body = data
res.status(200).send(data)
})
})
app.get('/savedCoupons', (req, res) => {
db.Coupons.findAll({where: {saved: 'true'}, limit: 20}).then((data) =>{
res.status(200).send(data)
})
})
app.post('/yes', (req, res) => {
db.Coupons.update({saved: 'true'}, {where: {id: req.body.id}}).then((data) => {
res.status(201).send('updated to true')
})
})
app.post('/no', (req, res) => {
db.Coupons.update({saved: 'false'}, {where: {id: req.body.id}}).then((data) => {
res.status(201).send('updated to false')
})
})
app.set('port', process.env.PORT || 3000)
const server = app.listen(app.get('port'))
console.log(server)
const host = server.address().address;
const port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);