npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-json-views

v0.2.2

Published

A JSON view engine for express

Downloads

746

Readme

express-json-views

A JSON view engine for the express framework to render objects and arrays with views written in JSON and helper functions.

npm version Build Status

Motivation

Even though express can send JSON responses out of the box by just retuning, in a lot of cases the response object or database models needs to be formatted. Think like DTOs in Java. Unnecessary or sensitive information like passwords need to be omitted, values need to be formatted and sub-objects/lists need to be altered as well.

Features

  • Recursively render objects and arrays
  • Omit values
  • Rename object keys
  • Format values using helpers
  • Sub views and lists
  • View caching

Setup

$ npm install express-json-views
$ yarn add express-json-views
var app = require('express')();
var viewEngine = require('express-json-views');

app.engine('json', viewEngine({
       helpers: require('./views/helpers')
   }));
app.set('views', __dirname + '/views');
app.set('view engine', 'json');

Features & examples

Defining views

Views are defined using JSON files and located in a single directory in your project.

{
	"id": {},
	"slug": {
		"format": "getPostSlug"
	},
	"title": {},
	"content": {
		"from": "content_text"
	},
	"author": {
		"view": "user"
	},
	"comments": {
		"view": "comment"
	},
	"published_date": {}
}

The keys of the view are the keys which will be rendered into to resulting object. The default configuration {} just copies the value from the passed data. Possible configurations are:

  • from: Uses this value to lookup the value in the data object instead of the view key.
  • format: Calls a helper function with this name. Passes the value and the full object as arguments.
  • view: Defines the view if this value should be rendered with a different view. Value must be an Array or objects or an Object.

Rendering objects and lists

You don't have to worry about whether you want to render a single object or a list of objects. The view engine detects if you pass an array and will iterate over it. call res.render(...) and pass the view name and an object with a property called data which contains your data.


app.get('/posts', function (req, res) {

	var posts; // Query from database

	res.render('post', {
		data: posts
	});

});

Helpers

To define helpers you pass an object of functions when creating the view engine.

var helpers = {
	date: function (value, object) {
		if (!value) {
        	return null;
        }
        return dateFormat(value, 'yyyy-mm-dd');
	}
};

app.engine('json', viewEngine({ helpers: helpers }));

Helper functions get 2 arguments, value and the full object. To calculate a value based on other values in the object you can use the second argument.

var helpers = {
	comment_count: function (value, object) {
		// Value will be null since this is a dynamic property and not present in the data object
		return object.comments ? object.comments.length : 0;
	}
};

If you return a Promise the view engine will resolve use it. Though I would strongly advise you, not to do async calls here.

Test

$ npm test