-
Notifications
You must be signed in to change notification settings - Fork 26
/
app.js
61 lines (51 loc) · 1.76 KB
/
app.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
// Do not remove! This file needed for uses library offline.
var fs = require('fs');
var express = require('express');
var requestJson = require('request-json');
var objectAssign = require('object-assign');
var app = express();
app.use('/modules', express.static(__dirname + '/modules'));
app.use('/binaries', express.static(__dirname + '/binaries'));
app.get('/', function(req, res) {
res.send('Hello World!');
});
app.get('/json/boards.json', function(req, res) {
var client = requestJson.createClient('http://espruino.com/');
client.get('json/boards.json', function(err, _, originalJson) {
if (err) {
originalJson = {};
}
fs.readFile(__dirname + '/json/boards.json', function(err, data) {
var localJson = JSON.parse(data);
var resultJson = objectAssign(originalJson, localJson);
res.send(resultJson);
});
});
});
app.get('/json/*', function(req, res) {
var root = __dirname + '/json';
var filename = req.params[0];
fs.access(root + '/' + filename, fs.F_OK, function(err) {
if (err) {
res.redirect('http://espruino.com/json/' + filename);
} else {
res.sendFile(filename, { root: root });
}
});
});
// The 404 Route
app.get('*', function(req, res) {
if (req.url.indexOf('amperka') > -1 || req.url.indexOf('@') > -1) {
console.log('Not found:', req.url);
res.status(404).send('Not found');
} else if (req.originalUrl.indexOf('/modules/') === 0) {
var module = req.originalUrl.substr('/modules/'.length);
console.log('Redirect for:', module);
res.redirect('http://espruino.com/modules/' + module);
}
});
var server = app.listen(3001, function() {
var host = server.address().address;
var port = server.address().port;
console.log('App listening at http://%s:%s', host, port);
});