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-autoapi

v1.0.0

Published

Auto-generates api routes based on the file system

Downloads

2

Readme

express-autoapi

Build Status

A node module for Express that automatically creates routes based on files in a folder. Point the api at a folder containing JavaScript files that contain API routes, and the routes will be automatically loaded and included in the routing system.

The initial use-case was for quickly building a Javascript API based simply on the files that exist in a folder, but they could be used to return any kind of result.

Installation

Install from Node Package Manager: npm install express-autoapi --save

Quick start

Include the module: var autoapi = require('express-autoapi');

and set up the routes:

autoapi.setup({
    app: app,    // The express app instance
    source: path.join(__dirname, 'api') // the path to find the api files
});

Given the following folder structure in the root of your node app:

/api
 |-- test.js
 |-- products.js
 |-- ham.js
 |-- users
  	 --/ index.js
  	 --/ admin.js

The following base roots will be created:

/api/test
/api/products
/api/ham
/api/users
/api/users/admin

Inside the route files, simply manipulate the Express Router in the same way that you normally would when creating routes for an application:

// test.js
var express = require('express');
var router = express.Router();

router.get('/', function(req, res) {
	console.log("Api hit..");
	res.send({ message: 'Hello from /api/test!' });
});

// Serve /api/test/12
router.get('/:id', function(req, res) {
    res.send({ id: req.params.id });
});

module.exports = router;

Thus, this module can be used to work with any existing application where the routing files are kept together in a folder.

Applying routes to the root url

By default, creating a module with the name index.js inside your api source folder will cause the routes to be applied to /api instead of /api/<module name>. This default name can be configured using the rootModule setting:

autoapi.setup({
    app: app,    // The express app instance
    source: path.join(__dirname, 'api'), // the path to find the api files
    rootModule: 'test'  // cause all roots in 'test.js' to exist on '/api' instead of '/api/test'
});

Options

app

required - sets the express application instance to use when defining the roots (required)

source

required - sets the folder to look for routing files (required). Should be an absolute path, or anything that is accepted by fs.readFileSync.

root

optional - sets the url root of the api. Default is '/api'

rootModule

optional - nominates a module name to use as the 'default', where the routes are applied to the api root. Default is 'index'.

debug

optional - enables debug mode (false by default), which writes out debug information to the console about the files that it has found to generate the routing information from and their urls.