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

v0.1.0

Published

Express dynamic api rendering middleware

Downloads

10

Readme

Deprecated

This module has renamed to dynapi, no longer maintained.

express-apis

Express dynamic api rendering middleware for more comfortable experience

* This package is not fully completed. If you have any suggestion, welcome to make issues for me ฅ' ω 'ฅ

Introduction

Express-apis is a powerful devtool for building an Express project quickly.

In default, express-apis watches all files in your api directory and mounts them under /api path. When you modify your file, express-apis will re-render the file and you would not have to restart your server again and again.

Quick start

Install the package

$ yarn add express-apis

Use express-apis in your app

import express from 'express'
import apis from 'express-apis'

const app = express()
app.use(apis())

Write you APIs like a boss!

// example API routes
project/
|- api/
|  |- middleware.js          // global middleware
|  |- get.js                 // GET: /api
|  |- post.js                // POST: /api
|  |- all.js                 // All methods: /api
|  |- user/_id/
|  |  |- middleware.js       // middleware used to /api/user/:id
|  |  |- param.js            // import User from '~controller/user-model' and checks user id exists or not
|  |  |- put.js              // PUT: /api/user/:id
|  |  |- delete.js           // DELETE: /api/user/:id
|  |  '- getProfile.js       // GET: /api/user/:id/profile
|  |- getUsers.js            // GET: /api/users
|  |- reportSystem_level.js  // REPORT: /api/system/:level
|  '- move_from_to.js        // MOVE: /api/:from/:to
'- controller/               // alias to '~controller'
   '- user-model.js          // import User from '~controller/user-model'

Feature

  • ES6/ES2015 syntax supported
  • Fully customizable options
  • Rich route patterns to keep api folder clean
  • Customizable allowed methods and matching order
  • Auto rejects request with timeout if middlewares spend too much time
  • Build once and rebuild on file changed keeps performance good
  • (Coming soon) Customize 404 response when no routes matched
  • (Coming soon) Customize 403 response when calling next(Error) in middlewares
  • (Coming soon) Add new file type error to allow users handle errors them self

Route file patterns

In default, you can use both simple patterns and complex patterns at the same time.

Simple patterns only contains three types of filename and two types of dirname:

Dirnames

  • (Starts without '_') - regular route

Express - Basic routing

The route name will be the kebab-case of the directory name.

Example: dirName(recommend), dir-name, and dir_name will be treated as same as /dir-name.

  • (Starts with '_') - param route

Express - Route parameters

The param name will be the snake_case of the directory name.

Example: _param_name(recommand), _paramName, and _param-name will be the same.

Filenames

  • middleware.js - middleware file

The middleware file can exists in any path, every param file and method file in the same or sub directories will always pass through the middleware file.

The middleware file file should always export a default function and contains three parameters like (req, res, next).

Always call the next() function, althrough headers is sent.

You can also provide a argument (expect an Error object) to reject the request, it will send a 403 status and { message: err.message } as response.

Express - Writing middleware

  • param.js - param file

It's similar to middleware, but you should always provide four parameters like (req, res, next, param).

The param.js file should always be the direct child of param route like user/_id/param.js, and the forth argument will be req.params['id'].

Express - Route parameters

  • [METHOD].js - method file

The [METHOD] can be anything in the options.allowedMethods array.

If the [METHOD] is not in options.allowMethods, you'll get an warning and the method file will be ignored.

All the [METHOD] of an method files should always in all lowercase. If the [METHOD] has a '-', ignore '-' and still keep all lowercase.

Express - Method

Complex patterns

  • TODO

API

TODO

import { Apis } from 'express-apis'

const apis = new Apis({ /* options */ })
// TODO

app.use(apis.render)

Options

import { Apis } from 'express-apis'

const apis = new Apis({
  rootDir: String, /* Default: process.cwd()
    The project root.
    `apiDir` and `controllerDir` is relative to this option */

  apiDir: String, /* Default: 'api'
    The API directory relative to `rootDir`. */

  controllerDir: String, /* Default: 'controller'
    The controller directory relative to `rootDir`.
    It will be resolved as `~controller` which imports by files in `apiDir` */

  apiRoot: String, /* Default: '/api'
    The API mounting point to the application. */

  dev: Boolean, /* Default: true
    Enable dev-mode or not.
    This option has no actual effect now.
    Prepare to used to generate static route files in production mode */

  timeout: Number, /* Default: 800
    If middleware doesn't call next(), reject the request after `timeout`ms.
    If timeout === 0, reject any request immediately.
    If timeout < 0, waits the middleware forever until next() has been called */

  allowedMethods: [String] /* Default: See below
    Enabled methods of request. */
})

Methods provided by express

ES2015 Syntax Support

Express-apis using babel-core to compile files, preset es2015 and stage-2, plugin async-to-generator and transform-runtime are in used.