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

js-controller

v0.3.6

Published

Use controllers to handle http requests

Downloads

22

Readme

Web Services Rapid Development

HTTP server side development for small or personal projects in a MVC-like way. In fact, it is suitable for most of the daily projects, I have used it to develop a number of online stable projects, very simple. The underlying framework of the HTTP service is based on Koa

Installation

npm i js-controller

Usage

// Import Library
import { runApp } from 'js-controller';
// Import configuration
runApp({
  controllerRoot: 'path/to/controller',
});

runApp accepts an object of type AppConfig to configure the application, all optional except for the controllerRoot parameter, which is mandatory

interface AppConfig {
  // X-Forwarded-Host is supported when proxy is true, default is true
  proxy?: boolean;
  // Application Listening Port, default is 9000
  port?: number | string;
  // Controller root directory
  controllerRoot: string;
  // The default action is `index`
  defaultAction?: string;
  // action prefix hook name, default is 'beforeAction'
  beforeAction?: string;
  // Options for koa-body
  bodyOptions?: IKoaBodyOptions;
  // A function that is triggered when the application starts and is used to initialize some functions
  onStart?: () => Promise<void> | void;
  // custom middlewares for koa
  middlewares?: Middleware[];
}

For a complete example, please refer to the project under example directory

Controller Conventions

  • The controller is a JavaScript file
  • The controller file must have a default class export
  • The instance methods of the controller class are agreed to be actions
  • The controller file name and the action together form the route, which is case-insensitive
  • Actions can have a default value, which is configured in the startup portal via defaultAction or, if not specified, index.

For example: /module/article/search

The last part of the path search will be treated as the action name, and the previous part /module/article will be treated as the path to the controller file, which will eventually resolve to

  • Controller file: path/to/[controllerRootDir]/module/article.js
  • Action name: search

The corresponding controller file should look like the following, which can be found in the [example](. /example) directory for an example

// ES module. Controller file: article.ts
// Since routing is not case sensitive, the file names article.ts, Article.ts, arTicle.ts are fine
export default class {
  search() {
    // TODO
  }
}

Controller filenames and action names are case-insensitive:

/module/ArtIcle/search  legal, controller is not case sensitive
/module/ArtIcle/seArch  legal, controller and action are case insensitive
/module/article/SeaRch  legal, actions are case insensitive

If search is configured as the default action, then the routes /module/article/search and /module/article are equivalent

Routing rules

path/to/[controllerRoot]/controller/action

The corresponding route is controller/action

path/to/[controllerRoot]/subdirectory/controller/action

The corresponding route is subdirectory/controller/action

path/to/[controllerRoot]/subdirectory/.../controller/action

The corresponding route is subdirectory/.../controller/action, Subdirectories can have unlimited levels

The directory structure is shown below.

┣━controllerRoot
  ┣━controller1.js
  ┣━controller2.js
  ┣━subdirectory
    ┣━controller1.js
    ┣━controller2.js
    ┣━...
  ┣━...

Development

Clone the project, run it at the same time

npm run ts
npm run dev