Skip to content

Commit

Permalink
Merge pull request #79 from amperka/fix-undo-app-js-remove
Browse files Browse the repository at this point in the history
Fix undo app.js removing
  • Loading branch information
yurybotov authored Nov 22, 2019
2 parents d954570 + 9b0d8d4 commit 964501e
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 964501e

Please sign in to comment.