routes.jssrc/routes/ | |
|---|---|
Routes provides the endpoints for our serverside. That is, it defines URLs that, when called on this server, will run custom code of our own. We use this for one purpose - to serve up JSON data about different stories to the client app. | |
Include our datasource, which loads up stories from disk, parses them, and provides database-like functions to access them | var RawArticleProvider = require('./../providers/RawArticleProvider.js');
var articleProvider = new RawArticleProvider(); |
This function handles any | var list_articles = function(req, res, next) {
articleProvider.getTitles(function(err,data) {
res.json(data);
});
}; |
This function handles any | var get_article = function(req, res, next) {
var id = parseInt(req.params.id);
if (isNaN(id)) {
next(new Error("ID is incorrect format"));
} else {
articleProvider.findById(req.params.id, function(err,data) {
if (err) {
next(err);
} else {
res.json(data);
}
});
}
} |
ENTRYPOINT
| exports.init = function(app) { |
Load up articles | articleProvider.loadDir(app.get('articleDir'));
app.get('/articles', list_articles);
app.get('/articles/:id', get_article);
} |
See src/providers/RawArticleProvider.js Next |