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 🙏

© 2025 – Pkg Stats / Ryan Hefner

eobject

v0.3.4

Published

Generate Express.js routes easily and automatically from a object.

Downloads

5

Readme

eobject

eobject is a package designed to quickly and easily create a API from a JavaScript object.

Compatible with all data types valid in JavaScript objects, eobject automatically generates Express.js routes.

| JSON | Web | |--|--| | A JSON Object | example.com/info - {name:"Test",version: "1.0.0"} example.com/info/name - Test example.com/info/version - 1.0.0 example.com/users - {} example.com/users/add - nothing example.com/users/[email protected] - [email protected]

A note on functions

Functions do work in eobject. It is one of the primary use cases for eobject. Functions that have arguments map both to URL query strings, as well as the body of the function.

Learn more about the usage of functions in eobject in the dedicated section

Getting Started

  • Install the package: npm install eobject
  • Start with the setup function to provide the object: eobject.setup(*object*,*settings (optional)*);
  • Use the generator function in app.use();.
    To use on the root: app.use(eobject.generator);
    To use on a directory: app.use('/*path*',eobject.generator);
  • Then, it should be accessible. Ex: *object*.info.name should corespond to /info/name

See the full working example for more guidance.

Docs

eobject.setup( object , ?settings )

The setup function configures eobject with the object that it should generate routes from. It accepts a JavaScript object and a optional settings object.

Example

eobject.setup({test:"test"})

var object = {test: "test"}
var settings = {debug: true}
eobject.setup(object,settings)

eobject.generator( req , res , next )

Important: The generator function is a Express.js middleware function and shouldn't be used directory.
To only be used within: app.use(eobject.generator) or app.use('*PATH*',eobject.generator)

Example

app.use('/api',eobject.generator)

app.use(eobject.generator)

Settings Documentation

Default Settings Object

{
  debug: false,
  secureonly: false,
  rootpagedisplay: true,
  ifnotfoundsendtonext: true,
  ifemptyobjectstillsend: true,
  acceptqueryfunctionarguments: true,
  acceptbodyfunctionarguments: false,
  requestdata: false,
  responsedata: false,
  executefunctionintrycatch: true,
  functionerrormessage: (err) => {return {error:err}},
  validtypes: {
    function: true,
    object: true,
    string: true,
    number: true
  }
}

Settings Options

The following table specifies each settings option: (See above for default settings)

| Name | Description | | --- | --- | | debug | Option for debug mode. It will show the process behind each and every request. | | secureonly | Option for allowing secure connections only. Only HTTPS connections will be allowed and anything else will be rejected by a 403 Forbidden response. | | rootpagedisplay | Option to display the root 'welcome' screen. | | ifnotfoundsendtonext | Option to send any unresolved requests beyond eobject to other Express.js routes. | | ifemptyobjectstillsend | Option to still send any empty objects (empty object: {}) back to the client. | | acceptqueryfunctionarguments | Option to disable accepting function arguments from URL query strings. | | acceptbodyfunctionarguments | Option to enable accepting function arguments from the request body. (See: "Pulling Arguments From the Query String of the Requested URL") | | requestdata | Option to provide the Express.js request (req) object as the last parameter in a given function, if said last parameter is named req. | | executefunctionintrycatch | Option to execute functions in a try/catch, where eobject will return a error message (which can be configured in the functionerrormessage option, see below) or not. If the function is not executed in a try/catch, if the function throws an error, the entire program will exit. | | functionerrormessage | A function that configures the error message that is displayed when a function returns an error. | | validtypes | Options to independently disable any specific data type from being used in eobject. |

Functions

Although all valid data types that work in JavaScript objects do work in eobject, functions are their own special data type which requires more work in order to be used properly.

For functions to effectively operate as an API, there must be methods of inputting function arguments from the client side. eobject has two ways of acomplishing this at this time:

  • URL Query Strings
  • Request Body

Pulling arguments from the query string of the requested URL

eobject by default pulls argument data from the query string of the URL that is requested via key/value combinations of the query string, parsed by the native URL constructor. You can disable pulling arguments from query strings via the acceptqueryfunctionarguments option in the [settings] (#settings-options).

The order of the query strings does not matter, but the name of the key must exactly match the name of the argument in the function, including the case.
Example:
If the argument name is firstName:
/users/[email protected] - Will Work
/users/[email protected] - Will Not Work
/users/[email protected] - Will Not Work

Thus, it is recommended that function names do be named in lower-case. In the future, function names will become case-insensitive.

Pulling arguments from the request body

eobject also has the option of pulling argument data from a JSON-formatted body of a HTTP request. This functionality must be enabled via the acceptbodyfunctionarguments option in the [settings] (#settings-options) in order to be used.

Although having a body for other types of requests is theoretically valid, eobject only accepts common practice request bodies in PUT, POST and PATCH requests.

Express also requires that the body-parser package be used to parse the request body. The full working example includes a implementation of this. It requires the following lines of code be included before the app.use(express.generator(*object*)) line and after app initalization of Express:

const bodyParser = require("body-parser");
app.use(bodyParser.json())

Example

Full working example:

const eobject = require('eobject');
const express = require('express');
const bodyParser = require("body-parser");
const app = express();

var object = {
  info: {
    name: "Test",
    version: '1.0.0'
  },
  users: {
    add: async function(firstName,lastName,email) {
      return {
        name: firstName + " " + lastName,
        email: email
      }
    }
  }
}

var settings = {
  "acceptbodyfunctionarguments": true,
  "secureonly": true
}

app.use(bodyParser.json())

eobject.setup(object,settings);
app.use('/',eobject.generator)

app.get('*',(req,res) => {
  res.send("test")
})

app.listen(3000, () => {
  console.log('server started');
});