-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
111 lines (99 loc) · 2.99 KB
/
Copy pathapp.js
File metadata and controls
111 lines (99 loc) · 2.99 KB
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
const restify = require('restify');
const fs = require('fs');
const path = require('path');
const cookieParser = require('restify-cookies');
// data store
const transactions = [];
const server = restify.createServer();
server.use(restify.plugins.queryParser());
server.use(restify.plugins.jsonBodyParser());
server.use(cookieParser.parse);
/*const corsHandler = (req, res, next) => {
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', '*');
return res.send(204);
}
};*/
//server.on('MethodNotAllowed', corsHandler);
server.use((req, res, next) => {
let magicToken = req.cookies['magictoken'];
if (req.path() === '/api/magiclogin' || !req.path().includes('/api')) {
return next();
} else if (!magicToken) {
res.send(403);
} else {
return next();
}
});
// redirect to reserve up the app if we navigate the browser to /app*
server.get('/app*', (req, res, next) => {
res.redirect(301, '/', next);
});
// serve up the client app entry point
server.get('/', (req, res, next) => {
// serve the client app index.html
let index = fs.readFileSync(path.join(__dirname, 'static/index.html'));
res.header('Content-Type', 'text/html');
res.header('Content-Length', index.byteLength);
res.status = '200';
res.end(index.toString());
});
// get static assets
server.get('/*', restify.plugins.serveStatic({
directory: './static',
}));
// ;-)
server.get('/api/sup', function (req, res, next) {
res.send('Yo whaddup?');
});
// loggin in
server.post('/api/magiclogin', (req, res, next) => {
res.header('Set-Cookie', `magictoken=${Math.random().toString(36) + Math.random().toString(36)}; SameSite=Lax`);
res.header('Content-Type', 'application/json');
res.send(200,{
'status': 'loggedin'
});
});
// sending dosh
const sendDosh = (recipient, amount) => {
transactions.push({
recipient: recipient,
amount: amount,
time: (new Date()).toDateString(),
});
};
server.post('/api/senddosh', (req, res, next) => {
let body = req.body;
if (req.header('Content-Type').includes('application/x-www-urlencoded')) {
let b = body.toString();
let p = {};
b.split('&').map((item) => {let v = item.split('='); p[v[0]] = v[1]});
body = p;
}
console.log(body);
sendDosh(body.recipient, body.amount);
res.header('Content-Type', 'application/json');
res.send(200, {
'status': 'money_sent',
});
});
server.get('/api/senddosh', (req, res, next) => {
let query = req.query;
sendDosh(query.recipient, query.amount);
res.header('Content-Type', 'application/json');
res.send(200, {
'status': 'money_sent',
});
});
server.get('/api/transactions', (req, res, next) => {
let cookie = req.cookies['magicToken'];
res.header('Content-Type', 'application/json');
res.send(200, {
transactions: transactions,
});
});
server.listen(8000, function() {
console.log('%s listening at %s', server.name, server.url);
});