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-autoroute-json

v3.6.3

Published

Express Auto Route Json is an automatic JSON 'REST' api builder for express-autoroute

Downloads

31

Readme

Build Status dependencies Status devDependencies Status Maintainability Test Coverage

Express-Autoroute-JSON - automatically define your JSON:API backend

express-autoroute-json is a handy tool which allows you to declarativly define endpoints for ExpressJS that speak JSON:API natively. It is designed to make use of Convention Over Configuration and your endpoints should have Zero boilerplate code, and only contain the definition of your business logic.

Quick-start

We have a handy Yeaman generator that will spin up a fully functioning backend for you in an instant. To get started, install yeoman globally and install the @authmaker/generator-express generator globally. Don't worry if you're not using Authmaker, you can still use this generator to quickly spin up your app.

npm install -g yo
npm install -g @authmaker/generator-express

the generator does not generate a folder for you so create one and run the generator in that folder:

mkdir my-app-backend
cd my-app-backend
yo @authmaker/express

the generator will ask you a series of questions to get started, and it will also ask you for a MongoDB connection string and setup that database connection for you.

Route definitions

If you have run the above quick-start steps you will have a file server/routes/v1/example.js that looks like this:

const autorouteJson = require('express-autoroute-json');
const { models } = require('../../../models');

module.exports.autoroute = autorouteJson({
  model: models.example,
  resource: 'example', // this will be pluralised in the routes

  // default CRUD
  find: {},
  create: {},
  update: {},
  delete: {},
});

there are a few things to note about this example. Firstly this is a fully functioning example that will create endpoints to create, retrieve, update and delete 'example' resources. When you run the server it will show the following output

info: creating endpoint: /v1/examples      #find all     - GET
info: creating endpoint: /v1/examples/:id  #find by id   - GET
info: creating endpoint: /v1/examples      #create       - POST
info: creating endpoint: /v1/examples/:id  #update       - PATCH
info: creating endpoint: /v1/examples/:id  #delete       - DELETE

you will also notice that the endpoints are prefixed with /v1/. This is because express-autroute-json is based on express-autoroute which is designed to give you a nicer way to describe your node endpoints and can auto-prefix endpoints with the folder-names they are contained in.

If you want to create a find-only endpoint i.e. you don't want to allow for the creation or deletion of resources then you can just remove the corresponding create, update and delete blocks from the autoroute definition.

const autorouteJson = require('express-autoroute-json');
const { models } = require('../../../models');

module.exports.autoroute = autorouteJson({
  model: models.example,
  resource: 'example', // this will be pluralised in the routes
  find: {},
});

will result in:

info: creating endpoint: /v1/examples      #find all     - GET
info: creating endpoint: /v1/examples/:id  #find by id   - GET

Customising the business logic

The simplest example of business logic that you might need for these endpoints is the ability to define authentication. Here is a simple example that restricts all endpoints in this autoroute definition to just be accessible to admins.

const autorouteJson = require('express-autoroute-json');
const { models } = require('../../../models');

function isAdmin(req, res, next) {
    //deny access if the user is not admin
    if (!req.user || !req.user.isAdmin) {
        return res.status(401).send("You are not an admin");
    }
    next();
}

module.exports.autoroute = autorouteJson({
  model: models.example,
  resource: 'example',

  // defining authentication here auto-applies it to all endpoints in this autoroute definition
  authentication: isAdmin,

  find: {},
  create: {},
  update: {},
  delete: {},
});

If you wanted to make it so that only admins are allowed to create, update or delete resources but everyone is able to retrieve resources then we can define authentication on each action block independently:

const autorouteJson = require('express-autoroute-json');
const { models } = require('../../../models');

function isAdmin(req, res, next) {
    if (!req.user || !req.user.isAdmin) {
        return res.status(401).send("You are not an admin");
    }
    next();
}

module.exports.autoroute = autorouteJson({
  model: models.example,
  resource: 'example',

  find: {},
  create: {
    authentication: isAdmin,
  },
  update: {
    authentication: isAdmin,
  },
  delete: {
    authentication: isAdmin,
  },
});

Further Documentation

We are in the process of developing some more in-depth documentation (including courses) as part of the Authmaker Curriculum. As I said above you do not need to use Authmaker if you want to to use express-autoroute-json, however the current Authmaker documentation is the most complete documentation of using this entire system in a full-stack application.

There is a tiny bit more documentation on the wiki but if you have any questions or want to request some specific documentation you can reach out to me on Twitter

Licence

Copyright (c) 2018, Stone Circle [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.