Skip to content

Commit

Permalink
Fix undo app.js removing
Browse files Browse the repository at this point in the history
  • Loading branch information
yurybotov committed Nov 22, 2019
1 parent d954570 commit 9b0d8d4
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
});

0 comments on commit 9b0d8d4

Please sign in to comment.