spee.ch/routes/html-routes.js

63 lines
1.9 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');
2017-06-13 00:57:14 +02:00
module.exports = function(app, visitor){
// route to fetch all free public claims
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-13 00:57:14 +02:00
visitor.pageview(req.url).send();
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){
2017-06-10 01:46:57 +02:00
console.log("/:name/all success.");
res.status(200).render('allClaims', { claims: orderedFreePublicClaims });
2017-06-01 01:44:45 +02:00
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-13 00:57:14 +02:00
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-05 02:40:06 +02:00
console.log("/:name/:claim_id 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", function(req, res){
2017-06-13 00:57:14 +02:00
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){
2017-06-05 02:40:06 +02:00
console.log("/:name error:", 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-13 00:57:14 +02:00
visitor.pageview(req.url).send();
2017-06-10 01:46:57 +02:00
res.status(200).render('index');
});
// a catch-all route if someone visits a page that does not exist
app.use("*", function(req, res){
2017-06-13 00:57:14 +02:00
visitor.pageview(req.url).send();
2017-06-10 01:46:57 +02:00
res.status(404).render('fourOhFour');
});
}