spee.ch/routes/html-routes.js

63 lines
2.1 KiB
JavaScript
Raw Normal View History

var path = require('path');
2017-06-03 09:41:02 +02:00
var routeHelpers = require('../helpers/routeHelpers.js');
var lbryApi = require('../helpers/lbryApi.js');
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");
2017-06-03 05:56:33 +02:00
res.sendFile(path.join(__dirname, '../public/assets/img', 'favicon.ico'));
});
// route to fetch one free public claim
app.get("/:name/all", function(req, res){
2017-06-03 05:56:33 +02:00
console.log(">> GET request on /" + req.params.name + " (all)");
2017-06-01 02:00:13 +02:00
// create promise
2017-06-03 05:56:33 +02:00
lbryApi.getAllClaims(req.params.name)
.then(function(orderedFreePublicClaims){
console.log("/:name/all success.")
2017-06-01 01:44:45 +02:00
res.status(200).send(orderedFreePublicClaims);
return;
})
.catch(function(error){
2017-06-03 05:56:33 +02:00
console.log("/:name/all error:", error);
2017-06-03 09:41:02 +02:00
routeHelpers.handleRequestError(error, res);
2017-06-01 01:44:45 +02:00
})
});
// 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-03 05:56:33 +02:00
lbryApi.getClaimBasedOnUri(uri)
.then(function(filePath){
console.log("/:name/:claim_id success.");
2017-06-01 01:44:45 +02:00
res.status(200).sendFile(filePath);
})
.catch(function(error){
2017-06-03 05:56:33 +02:00
console.log("/:name/:claim_id error.")
2017-06-03 09:41:02 +02:00
routeHelpers.handleRequestError(error, res);
2017-06-01 01:44:45 +02:00
});
});
// route to fetch one free public claim
app.get("/:name", function(req, res){
2017-06-03 05:56:33 +02:00
console.log(">> GET request on /" + req.params.name);
2017-06-01 02:00:13 +02:00
// create promise
2017-06-03 05:56:33 +02:00
lbryApi.getClaimBasedOnNameOnly(req.params.name)
.then(function(filePath){
console.log("/:name success.")
2017-06-01 01:44:45 +02:00
res.status(200).sendFile(filePath);
2017-06-03 05:56:33 +02:00
}).catch(function(error){
console.log("/:name error.");
2017-06-03 09:41:02 +02:00
routeHelpers.handleRequestError(error, res);
2017-06-01 01:44:45 +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'));
});
// 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'));
});
}