-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
128 lines (114 loc) · 3.71 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const express = require('express');
const path = require('path');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const APP_PORT = 3000;
const REST_PORT = 8080;
// restful endpoint
const restServer = express()
const crypto = require('crypto');
const sqlite3 = require('sqlite3').verbose();
const jwt = require('jsonwebtoken');
const expressJWT = require('express-jwt');
const generateHash = (data) => {
return crypto.createHash('md5')
.update(data)
.digest('hex');
}
var productETag;
restServer.get('/product/:slug', (req, res) => {
if (!productETag || req.headers["if-none-match"] !== productETag){
var db = new sqlite3.Database('products.db');
db.all('select * from products where slug = ?', [req.params.slug], (err, rows) => {
if (err) throw err;
productETag = generateHash(Buffer.from(rows));
res.set({"ETag": productETag});
res.json(rows);
});
db.close((err) => {
if (err) throw err;
console.log('fetch api/product/'+ req.params.slug +' finished');
});
}else{
// Here is using HTTP Cache to avoid refetching database
console.log("product "+ req.params.slug +" is using browser cache");
res.set({"ETag": productETag});
res.send();
}
});
restServer.get('/id/:num', (req, res) => {
var db = new sqlite3.Database('products.db');
db.get('select * from products where id = ?', [req.params.num], (err, row) => {
if (err) throw err;
res.json(row);
});
db.close((err) => {
if (err) throw err;
console.log('fetch api/id/'+ req.params.num +' finished');
});
})
var rowsETag;
restServer.get('/products', (req, res) => {
if (!rowsETag || req.headers["if-none-match"] !== rowsETag){
var db = new sqlite3.Database('products.db');
db.all('select * from products', (err, rows) => {
if (err) throw err;
rowsETag = generateHash(Buffer.from(rows));
res.set({"ETag": rowsETag});
res.json(rows);
});
db.close((err) => {
if (err) throw err;
console.log("fetch all products finished");
});
} else {
// Here is using HTTP Cache to avoid refetching database
console.log("products list is using browser cache")
res.set({"ETag": rowsETag});
res.send();
}
})
restServer.post('/userid/:id/password/:pwd', (req, res) => {
if(req.params.id === '348689' && req.params.pwd === 'onlineretail'){
var token = jwt.sign({
userid: req.params.id,
name: 'caasiu',
}, 'caasiu online-retail demo');
res.send(token);
}else{
res.sendStatus(401);
}
})
restServer.get('/account',
expressJWT({secret: 'caasiu online-retail demo'}),
(req, res) => {
if(!req.user) return res.sendStatus(401);
res.json({
userid: req.user.userid,
name: req.user.name,
address: 'NO 3, Street 7',
balance: 200
});
}
)
restServer.listen(REST_PORT, () => {
`Restful Server is running on http://localhost:${REST_PORT}`;
})
const config = require("./webpack.config.js");
const compiler = webpack(config);
const app = new WebpackDevServer(compiler, {
contentBase: "/src/",
hot: true,
historyApiFallback: true,
proxy: {'/api/**': {
target: `http://localhost:${REST_PORT}`,
secure: false,
pathRewrite: {'^/api': ''}
}},
stats: {colors: true}
});
//serve static resources
app.use('/', express.static(path.join(__dirname, 'src')));
app.listen(APP_PORT, () => {
console.log(`App is running on http://localhost:${APP_PORT}`);
})