Skip to content

chenjic215/CheeseToastie

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CheeseToastie ( 🧀 🍞 )

CheeseToastie is a NodeJS library that makes it really easy to build well thought out APIs based on Swagger JSON files.

Features

  • API defined via a JSON file (Swagger-like format)
  • Validation of parameters (type, required, etc)
  • Ability to include an authentication function that will run prior to secured endpoints being hit
  • Uses ExpressJS as an underlying library

JSON File

To use this library, you'll need to setup a JSON Swagger file that defines your API (endpoints, parameters, etc). More examples on this coming soon.

Quick Start

var express = require("express");
var bodyParser = require('body-parser');

var cheeseToastie = require("cheese-toastie");

var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// Start the CheeseToastie API
cheeseToastie.start(__dirname, app);

// Start the server
var server = app.listen(process.env.PORT || '8080', function () {
  console.log("You're ready to go 🧀 🍞");
});

Authenticating Requests

You can mark routes in your JSON file as requiring authentication like this:

...
"authRequired": true,
...

You must then provide an authenticator function to the CheeseToastie library that will run against authenticated requests and return either a user object or null (if the user is not logged in/their token is not valid).

cheeseToastie.setAuthenticatorMethod(function(token, callback) {
  // Silly example, normally you'd be decoding a JSON Web Token
  if (token && token == "1234") {
    return callback({name: "Daryl"});
  }

  return callback(null);
});

cheeseToastie.start(__dirname, app);

Callback options

When you return a callback to Cheese Toastie it should be of the following format:

return callback(err, data, redirect, options)
  • err - An error, if present
  • data - The data response, by default this should be in JSON
  • redirect - An optional redirect URL
  • options - An optional object that contains

About

Framework for building APIs with NodeJS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%