-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
68 lines (58 loc) · 2.05 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
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser')
const app = express();
const fs = require("fs");
app.use(require('morgan')('short'));
var jsonParser = bodyParser.json();
(function initWebpack() {
const webpack = require('webpack');
const webpackConfig = require('./webpack/common.config');
const compiler = webpack(webpackConfig);
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true, publicPath: webpackConfig.output.publicPath,
}));
app.use(require('webpack-hot-middleware')(compiler, {
log: console.log, path: '/__webpack_hmr', heartbeat: 10 * 1000,
}));
app.use(express.static(__dirname + '/'));
})();
app.post('/api/payment/', jsonParser, function(req, res) {
setTimeout(function() {
fs.readFile(__dirname + "/" + "api/payment.json", 'utf8', function(err, data) {
let obj = JSON.parse(data);
console.log(req.body);
for(var i=0; i < obj.length; i++) {
if(obj[i].number == req.body.number &&
obj[i].cvc == req.body.cvc &&
obj[i].expiration.year == req.body.year &&
obj[i].expiration.month == req.body.month) {
res.end('{"result": "valid"}');
}
}
res.end('{"result": "not valid"}');
})
}, 4000);
});
app.post('/api/coupon', jsonParser, function(req, res) {
setTimeout(function() {
fs.readFile(__dirname + "/" + "api/coupon.json", 'utf8', function(err, data) {
let obj = JSON.parse(data);
for(var i=0; i < obj.length; i++) {
if(obj[i] == req.body.coupon) {
res.end('{"result": "valid"}');
}
}
res.end('{"result": "not valid"}');
})
}, 4000); // simulate server latency
});
app.get(/.*/, function root(req, res) {
res.sendFile(__dirname + '/index.html');
});
const server = http.createServer(app);
server.listen(process.env.PORT || 3000, function onListen() {
const address = server.address();
console.log('Listening on: %j', address);
console.log(' -> that probably means: http://localhost:%d', address.port);
});