-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.js
More file actions
81 lines (62 loc) · 1.7 KB
/
api.js
File metadata and controls
81 lines (62 loc) · 1.7 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
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var crypto = require('crypto');
app.configure(function () {
app.set('port', 3000);
app.use(function(req, res, next){
console.log('%s %s', req.method, req.url);
next();
});
app.use(function (req, res, next) {
if (req.url === '/auth') {
return next();
}
if (req.headers['x-api-key'] !== 'abc123') {
return res.send(401, 'Unauthorized');
}
return next();
});
app.use(express.bodyParser());
});
server.listen(app.get('port'));
app.get('/', function (req, res) {
return res.send(200);
});
app.post('/auth', function (req, res) {
var credentials = req.body;
if (credentials) {
if (credentials.username === 'user' && credentials.password === 'password') {
res.setHeader('Content-Type', 'application/json');
return res.send({ token: 'abc123' });
}
else {
res.send(401, 'Unauthorized')
}
}
else {
return res.send(401, 'Unauthorized');
}
});
app.post('/transform', function (req, res) {
var obj = req.body;
var xout = req.query.xout;
Object.keys(obj).forEach(function (k) {
var result = obj[k];
if (xout) {
result = result.replace(new RegExp(xout, 'g'), 'X');
}
obj[k] = result.toUpperCase();
});
res.setHeader('Content-Type', 'application/json');
res.send(200, obj);
});
app.post('/hash', function (req, res) {
var md5sum = crypto.createHash('md5');
req.on('data', function (data) {
md5sum.update(data);
});
req.on('end', function () {
res.send({ hash: md5sum.digest('hex') });
});
});