How to manage data template when serving an api using Node.js

I'm currently trying to create an api for my web app using Node.js. The api intended to return JSON data to the api user.

For example, i'm having following endpoint / object: - Articles (collection of article) - Article (single article, each article would be having tags) - Tags

Each endpoint / object having they own json data format need to return, in example:

  • Articles: [{articleObject}, {articleObject}, {articleObject}]

  • Article

    { id: 12, title: 'My awesome article *i said and i know it', content: 'blah blah blah blah', tags: [{tagObject}, {tagObject}, {tagObject}] }

Each endpoint can call to other endpoint to get needed data, in example: Article endpoint can calling the Tags endpoint to get the article tags collection, the Tags endpoint will be returning Array Object.

My questions:

  1. How to manage the Object structures, so if a endpoint called from another endpoint it will return Array / Object, while it will be return JSON string when called from api user.

  2. Should I create Object template for each endpoint. The process will be:

    • Endpoint will be calling Object Template;
    • The Object Template will fetching data from database;
    • Endpoint will return the Object Template when called from other endpoint;
    • Endpoint will call template engine to parse the Object Template to JSON string for api user. JSON string view will be provided for each endpoint.
  3. What Template Engine can process below Object Template and treat them as variables, in PHP i'm using Twig that can receive PHP Object and treat them as variables. Here you can find how i do that using Twig

Object Template file content example:

var Tags = require('./tags');

module.exports = function(param){
  /* do fetching data from database for specified parameters */
  /* here */
  var data = {};

  return {
    id: function() {
      return data.id;
    },
    title: function() {
      return data.title;
    },
    description: function() {
      return data.description;
    },
    tags: function() {
      return Tags(data.id);
    }
  }
}

Hope someone can show me the way for it.

I use express to do my apis...you can do something like this:

app.get('/api/article', authenticate, routes.api.article.get);

authenticate is just a middleware function that authenticates the user, typically this is grabbing the user from session and making sure they are logged in.

inside ./routes/api.js

var Article = require('../models/article.js');

exports.api = {}
exports.api.article = {}
exports.api.article.get = function(req, res){
 //get article data here
 Article.find({created_by: req.session.user._id}).populate('tags').populate('created_by').exec(function(err, list){
   res.json(list);
 });
};

This assume you're using mongoose and mongodb and have an Article schema similar to this, which includes a reference to a Tag schema:

var ArticleSchema = new Schema({
    name    : { type: String, required: true, trim: true }
  , description: { type: String, trim: true }
  , tags  : [{ type: Schema.ObjectId, ref: 'Tag', index: true }]
  , created_by  : { type: Schema.ObjectId, ref: 'User', index: true }
  , created_at    : { type: Date }
  , updated_at    : { type: Date }
});