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

autobackend

v0.1.2

Published

Provides automatic Backend for express apps including user database authentication, api creation, and database abstraction

Downloads

56

Readme

AutoBackend

AutoBackend is batteries included backend configuration library for Express that automatically configure resources and API's with minimal effort. It supports multiple databases, and JSON Web Tokens by default. Using this library with a few lines of code, you will have user authentication and api endpoint protection with RESTful routes automatically configured, hence the name "AutoBackend".

Note... This library is ment for development project, or prototyping, and not for production, although it can be used in small environments. It's purpose is to automate the back-end logic, allowing the developer to focus on front-end implementation ie... (vue.js, angularjs, react).

Installation

npm install autobackend use the --save flag to include it in your package dependencies

Basic Usage

In your app.js file include the following code to setup AutoBackend

var autobackend = require("autobackend")()

Factory Generator

app.use('/', autobackend.factory());

This will load AutoBackend with the default configuration. Custom configuration can be loaded with a config object.
Below is the default configuration:

var config = {
	database:{
		type:"disk", // Options are disk, mongodb, mysql,sqlserver
		connection_string:"mongodb://localhost/test" // database connection string
	},
	jwt:{
		secret:"secret", // The secret used to create JSON Web Tokens
		header_key:"access_token" // Request header key for tokens to validate
	},
// This is the admin user that will be generated if the Factory Generator is called
	admin_user:{ 
		fname:"Website",
		lname:"Administrator",
		username:"admin",
		role:"admin",
		email:"[email protected]",
		password:"password"
	}
	
}

Using app.use('/', autobackend.factory()) will create the following routes:

/users - protected with JSON Web Token user access_token key in the request header.

  • RESTful routes for user
    • "GET" /users/:_id? Gets all or one user with the optional _id key passed either in the parameter or query string
    • "POST" /users Inserts one user to database. Note... username, and email must be unique.
    • "PUT" /users/:_id? Updates one user in database by _id. Note... _id can be in the body, parameter, or query string of request.
    • "DELETE" /users/:_id? Deletes one user from database by _id. Note... _id can be in the body, parameter, or query string of request.

/auth - Used for authentication and registration routes.

  • Routes for /auth
    • "POST" /auth/login - Authenticate user and respond with JSON Web Token and user information. Note... Username or Email and password must be supplied in the body of this request. This will authenticate from the Users collection. You can use the default admin user to gain access to protected routes
    • "POST" /auth/logout Logs out user and places a record in attempts collection.
    • "POST" /auth/register Creates a new user, but does not log them in
    • "POST" /auth/validate Validates the JSON Web Token in the request headers
    • "GET" /auth/attempts - Protected This will retrieve a log of login and logout attempts. Each time a user attempts to login, a log record will be placed in the attempts collection. This is useful for debugging and access control
    • "GET" /auth/jwts- Protected This will retrieve all the tokens issued to the users. For debugging.

Collection Generator

As you can see, one line of code saves you a tremendous amount of time and effort. You can also create custom flexible collections with RESTful routes by using autobackend.collection("[name of collection]");

app.use('/', autobackend.factory());
app.use("/todos", autobackend.collection("todos"));

In addition to the routes created by factory, you now have a /todos collection. You can pass any JSON object to these routes and it will be stored.

  • RESTful routes for todos
    • "GET" /todos/:_id? Gets all or one todo with the optional _id key passed either in the parameter or query string
    • "POST" /todos Inserts one todo to database.
    • "PUT" /todos/:_id? Updates one todoin database by _id. Note... _id can be in the body, parameter, or query string of request.
    • "DELETE" /todos/:_id? Deletes one todo from database by _id. Note... _id can be in the body, parameter, or query string of request.

Route Protection

AutoBackend also provides you middleware functions the enable you to protect any route you have with the JSON Web Tokens issued by autobackend using autobackend.middleware.verify middleware.

app.use("/todos", autobackend.middleware.verify, autobackend.collection("todos"));

This will protect the /todos route with JSON Web Tokens issued by autobacked. The route will respond with a 401 Unauthorized status if the token is invalid. Using this you can protect any route you wish with one line of code.

Configuration

You can supply your own configuration object to autobackend, allowing you to use a different database or jwt secret, or admin user credentials Note... disk db is the default database, and mongodb is supported as an option. More database support will come in the furture.

Below is a sample configuration object:

// config.js
module.exports = {
	database:{
		type:"mongodb", // Options are disk, mongodb, "rethinkdb, mysql, sqlite, couchdb, sqlsvr will come in the future"
		connection_string:"mongodb://localhost/autobackend" // database connection string
	},
	jwt:{
		secret:"mysupersecret", // The secret used to create JSON Web Tokens
		header_key:"access_token" // Request header key for tokens to validate
	},
// This is the admin user that will be generated if the Factory Generator is called
	admin_user:{ 
		fname:"Imma",
		lname:"Boss",
		username:"admin",
		role:"admin",
		email:"[email protected]",
		password:"supersecretpassword"
	}
	
}

You can use this object when calling autobackend

var config = require("./config");
var autobackend = require("autobackend")(config);

or like this!

var config = require("./config");
var autobackend = require("../index");

var ab = autobackend(config);

or like this!

var autobackend = require("autobackend")
var config = {
	database:{
		type:"mongodb", 
		connection_string:"mongodb://localhost/autobackend" 
	},
	jwt:{
		secret:"mysupersecret", 
		header_key:"access_token"
	},
	admin_user:{ 
		fname:"Imma",
		lname:"Boss",
		username:"admin",
		role:"admin",
		email:"[email protected]",
		password:"supersecretpassword"
	}
	
}
var ab = autobackend(config)

You may supply only the parts of the configuration that you need like so:

var config = {
		database:{
			type:"mongodb", 
			connection_string:"mongodb://localhost/autobackend" 
		}
}

Contribution

I'm open to ways to make this library better. Please try this out in your own environments and let me know what works and what doesnt. Keep a look out for the future for more databases to be supported.