spee.ch/routes/html-routes.js
2017-06-03 00:41:02 -07:00

62 lines
2.1 KiB
JavaScript

var path = require('path');
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");
res.sendFile(path.join(__dirname, '../public/assets/img', 'favicon.ico'));
});
// route to fetch one free public claim
app.get("/:name/all", function(req, res){
console.log(">> GET request on /" + req.params.name + " (all)");
// create promise
lbryApi.getAllClaims(req.params.name)
.then(function(orderedFreePublicClaims){
console.log("/:name/all success.")
res.status(200).send(orderedFreePublicClaims);
return;
})
.catch(function(error){
console.log("/:name/all error:", error);
routeHelpers.handleRequestError(error, res);
})
});
// route to fetch one free public claim
app.get("/:name/:claim_id", function(req, res){
var uri = req.params.name + "#" + req.params.claim_id;
console.log(">> GET request on /" + uri);
// create promise
lbryApi.getClaimBasedOnUri(uri)
.then(function(filePath){
console.log("/:name/:claim_id success.");
res.status(200).sendFile(filePath);
})
.catch(function(error){
console.log("/:name/:claim_id error.")
routeHelpers.handleRequestError(error, res);
});
});
// route to fetch one free public claim
app.get("/:name", function(req, res){
console.log(">> GET request on /" + req.params.name);
// create promise
lbryApi.getClaimBasedOnNameOnly(req.params.name)
.then(function(filePath){
console.log("/:name success.")
res.status(200).sendFile(filePath);
}).catch(function(error){
console.log("/:name error.");
routeHelpers.handleRequestError(error, res);
});
});
// route for the home page
app.get("/", function(req, res){
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){
res.status(404).sendFile(path.join(__dirname, '../public', 'fourOhfour.html'));
});
}