New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

jason-parse

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jason-parse

JSON to ES6 class converter middleware for express

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

Jason

JSON to ES6 class object converter middleware for express

Prerequisites

Jason uses the ES6 Reflect api, which requires at least Node.js version 6.4.0

Installing

npm install jason-parse

Demonstration

So first we define a basic model which requries an empty constructor that takes in parameter ...args

class User {
    constructor(...args){}
 
    get name() {
        return this.name;
    }
   
    set name(newName) {
        this.name = newName;
    }
}
module.exports = User;

When you POST json to the express server, send it to the parameter entity so that the middleware can parse it

Now for our express routes that we want to use this middleware on:

const routes = require('express').Router();
const User = require('./User');
const jason = require('../index');

routes.post('/<name>', jason.single(User, 'user'), function(req, res) {
    //req.body.user contains the parse entity from '{"name":"foo"}'
});

routes.post('/<name>', jason.array(User, 'users'), function(req, res) {
    //req.body.users contains an array of the parsed entities '[{"name":"foo"},{"name","bar"}]'
});

Authors

  • Tom Orth (atf1999)

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgements

  • Spring REST for doing this in their Java web framework and inspiring me to do this for Node.js

Keywords

jason

FAQs

Package last updated on 16 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts