spee.ch/routes/html-routes.js

101 lines
3.1 KiB
JavaScript
Raw Normal View History

// load dependencies
var path = require('path');
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
// load helpers
var lbryApi = require('../helpers/lbryApi.js');
var queueApi = require('../helpers/queueApi.js');
// routes to export
module.exports = function(app){
// route to fetch one free public claim
app.get("/favicon.ico", function(req, res){
console.log(" >> GET request on favicon.ico");
res.sendFile(path.join(__dirname, '../public', 'favicon.ico'));
});
// route to fetch one free public claim
app.get("/:name/all", function(req, res){
var name = req.params.name;
console.log(">> GET request on /" + name + " (all)");
2017-06-01 02:00:13 +02:00
// create promise
2017-06-01 01:44:45 +02:00
var promise = lbryApi.getAllClaims(name);
// handle the promise resolve
promise.then(function(orderedFreePublicClaims){
2017-06-01 02:00:13 +02:00
console.log("/name/all promise success.")
2017-06-01 01:44:45 +02:00
res.status(200).send(orderedFreePublicClaims);
return;
})
// handle the promise rejection
.catch(function(error){
2017-06-01 02:00:13 +02:00
console.log("/name/all/ promise error:", error);
2017-06-01 01:44:45 +02:00
// handle the error
if ((error === "NO_CLAIMS") || (error === "NO_FREE_PUBLIC_CLAIMS")){
res.status(307).sendFile(path.join(__dirname, '../public', 'noClaims.html'));
return;
} else {
res.status(400).send(error);
return;
};
})
});
// route to fetch one free public claim
app.get("/:name/:claim_id", function(req, res){
2017-06-01 01:44:45 +02:00
var uri = req.params.name + "#" + req.params.claim_id;
console.log(">> GET request on /" + uri);
2017-06-01 02:00:13 +02:00
// create promise
2017-06-01 01:44:45 +02:00
var promise = lbryApi.getClaimBasedOnUri(uri);
// handle the promise resolve
promise.then(function(filePath){
console.log("/name/claim_id promise success - filepath:", filePath)
res.status(200).sendFile(filePath);
return;
})
// handle the promise rejection
.catch(function(error){
console.log("/name/claim_id/ promise error:", error)
// handle the error
if (error === "Invalid URI") {
2017-06-01 06:12:43 +02:00
res.status(400).sendFile(path.join(__dirname, '../public', 'invalidUri.html'));
2017-06-01 01:44:45 +02:00
return;
} else {
res.status(400).send(error);
return;
};
});
});
2017-06-01 02:00:13 +02:00
// route to fetch one free public claim
app.get("/:name", function(req, res){
2017-06-01 01:44:45 +02:00
var name = req.params.name;
console.log(">> GET request on /" + name);
2017-06-01 02:00:13 +02:00
// create promise
2017-06-01 01:44:45 +02:00
var promise = lbryApi.getClaimBasedOnNameOnly(name);
// handle the promise resolve
promise.then(function(filePath){
console.log("/name promise success - filepath:", filePath)
res.status(200).sendFile(filePath);
return;
})
// handle the promise rejection
.catch(function(error){
console.log("/name/ promise error:", error);
// handle the error
if ((error === "NO_CLAIMS") || (error === "NO_FREE_PUBLIC_CLAIMS")){
res.status(307).sendFile(path.join(__dirname, '../public', 'noClaims.html'));
return;
};
2017-06-01 06:12:43 +02:00
res.status(400).send(error);
2017-06-01 01:44:45 +02:00
});
});
2017-06-01 02:00:13 +02:00
// route for the home page
app.get("/", function(req, res){
2017-06-01 01:44:45 +02:00
res.status(200).sendFile(path.join(__dirname, '../public', 'index.html'));
});
2017-05-26 09:49:29 +02:00
// a catch-all route if someone visits a page that does not exist
app.use("*", function(req, res){
2017-06-01 01:44:45 +02:00
res.status(404).sendFile(path.join(__dirname, '../public', 'fourOhfour.html'));
});
}