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

v2.0.0

Published

Express route decorators

Downloads

14

Readme

express-decorate

Build Status Coverage Status

#Why:

This library is based on several similar libraries available on npm, most notably route-decorators. The main difference with this library is that it makes use of Express' detached router capabilities. In other words, this library configures Express routes like this:

import express = require('express');
import { Router } from 'express';

let app:express.Application = express();

// Tell Express to use the specified mountpath
// This would map to a controller which handles all requests to the specified mountpath
app.use('/mountpath/:mountPathParam', routerFunction);

// Attach specific controller actions to endpoints of the above mountpath
function routerFunction():Router
{
    let router:Router = Router({ mergeParams: true });
    router.get('/endpointPath', controller.Action);
    return router;
}

The benefit of the above route configuration is that, if you depend on Express' req.originalUrl, req.baseUrl, req.path, etc, those values will be attached to your routes as expected. Additionally, the user can choose whether to merge mountpath parameters with endpoint parameters. E.g., in the above example, in a controller action that handled GET requests to the endpoint /:endpointParam, that controller method would have access to both req.params.mountPathParam and req.params.endpointParam.

###Install:

$ npm install --save express-decorate

###Usage:

import { ExpressDecorate, IExpressDecorateOptions } from 'express-decorate'
import express = require('express');

let APP = express();

const OPTS:IExpressDecorateOptions = {
    ctrlDir: `${__dirname}/api`, // Required
    extension: 'js', // Optional - defaults to 'js'
    ctrlLoadRecursive: true, // Optional - defaults to true - set to false if there are no subdirectories in your controllers directory
    ctrlIgnore: 'IgnoreThisController', // Optional - NOT TESTED - @type {string|RegExp}
    mergeParams: true, // Optional - defaults to true - access params used in @Controller in Express's req.params object
    routeConfig: null, // Optional - defaults to null - Any error handling or routes not included in your API
    debug: true, // Optional - defaults to false - Show stack trace on caught exceptions
    alternateMethod: 'websocket' // Optional - Useful if using a library that alters Express' router, (such as express-ws-routes for handling websocket requests)
}

new ExpressDecorate(APP, OPTS);

###API:

import { Controller, GET, ALT } from 'express-decorate'

@Controller('/api/controller/:name')
export class ApiController
{
    @GET('/:testParam')
    public async Get(req, res, next)
    {
        return res.json({ success: true, controllerNameParam: req.params.name, testParam: req.params.testParam });
    }
    
    @ALT('/websocket')
    public async WebSocketRequest(info:any, cb:Function, next:NextFunction)
    {
        console.log(`ws req from ${info.req.originalUrl || info.req.url} using origin ${info.origin}`);
    }
}

###Contributing:

This is my first attempt at any open source library and I welcome pull requests that conform stylistically to existing code and are covered adequately with unit tests.

####Install dependencies

npm install && typings install

####Build:

gulp build

####Test:

Test a specific file: gulp test --file SpecToTest (don't include path or extension)

Run all specs: gulp test (this command also runs gulp build)