API rendering latest post for each supported domain

parent 95c76ee2
......@@ -31,7 +31,34 @@ app.get( '/api', function( req, res ) {
} );
app.get( '/api/:domain', function( req, res ) {
res.json( { 'd': req.params.domain } );
// For convenience
var domain = req.params.domain;
// Validate the entry
if ( 'undefined' === typeof domains[ domain ] ) {
res.status( 400 ).json( { error: 'Invalid domain specified' } );
return;
}
// Build URL and create REST client
var url = domains[ domain ];
var endpoint = url + 'wp-json';
var client = new wpapi( { endpoint } );
// Retrieve the latest post
client.posts().perPage( 1 ).orderby( 'date' ).order( 'desc' ).then( function( data ) {
res.render( 'partials/post', {
data: {
domain: domain,
url: url,
post: data[0]
},
layout: 'single-post'
} );
} ).catch(function( err ) {
console.error( err );
res.status( 500 ).json( { error: 'API request failed' } );
} );
} );
// Listen on some random, unused port
......
<h3><a href="{{data.post.link}}">{{data.post.title.rendered}}</a></h3>
{{{data.post.excerpt.rendered}}}
<p><a href="{{data.post.link}}">Read at {{data.domain}}&rarr;</a></p>
{{#each data.domains}}
<div class="site-container" id="site-{{@key}}">
<h2><a href="{{this}}">{{this}}</a></h2>
<span class="latest-post"></span>
<span class="latest-post">Loading&hellip;</span>
</div>
{{/each}}
......@@ -9,5 +9,7 @@
<div id="sites">
{{> sites}}
</div>
{{> analytics}}
</body>
</html>
{{> post}}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment