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-mvc-ts

v0.5.1

Published

.NET MVC-like controller structure for Express.js in Typescript

Downloads

7

Readme

express-mvc-ts

.NET MVC-like controller structure for Express.js in Typescript

Word of advice

Since this project is fairly new and it changes alot even between minor versions, it's recommended that you set a specific version as dependency in your project.json.

Purpose

Brings the familiarity of .NET MVC Controllers to Express.js - with the help of Typescript - with some limitations.

Why?

Because for people accustomed to MVC frameworks, this:

app.get('/', function (req, res) {
    res.render('home/index');
});

app.get('/about', function (req, res) {
    res.render('home/about');
});

app.get('/contact(/:reason)?', function (req, res) {
    res.render('home/index', { reason: req.params.reason });
});

app.post('/contact', function (req, res) {
    myDataStore.saveContactRequest(req.body)
        .then(() => res.render('contact', { success: true }))
        .catch(() => res.render('contact', { success: false }));
});

doesn't look as nice as this:

@Route('')
export class HomeController extends Controller {

   private dataStore: MyCoolDataStore = new MyCoolDataStore();

   @HttpGet
   public index() {
       return this.view();
   }

   @HttpGet
   public about() {
       return this.view('about');
   }

   @HttpGet('contact(/:reason)?')
   public contact() {
       return this.view('contact', { reason: this.request.params.reason });
   }

   @HttpPost
   public contactPost() {
       return this.dataStore.saveContactRequest(this.request.body)
           .then(() => this.view('contact', { success: true }))
           .catch(() => this.view('contact', { success: false }));
   }

}

How?

This can be done thanks to the magic of Typescript decorators and reflection metadata. So this is quite unusable in simple javascript scenarios, hence the name suffix: -ts;

Usage

  • Add this package to your dependencies.
  • Grab the typescript definition file from the github page and put it in your projects definition folder( or however you manage your definitions). Not definitelytype/tsd support yet.
  • Create your controller classes in a controllers folder (configurable). They should look like this:
import {Controller, HttpGet, HttpPost} from 'express-mvc-ts';

export class MyCoolController extends Controller {

    @HttpGet
    public index() {
        return this.view();
    }

    @HttpGet
    public posted() {
        return this.view();
    }
}

The above example will bind the routes get('/mycool',...) and post('/mycool/posted',...). For more route annotations check out the definition file.

  • In your app.ts call:
import * as mvc from 'express-mvc-ts';
//initialize app
mvc.setup(app);
app.listn();

Caveats / Todos

  • The default value for the view response is always index, this should be the name of the method, like in .NET MVC.
  • In case async work is done inside a method, the this.request and this.response values need to be cached before doing any async calls, as simultaneous request will overwrite the values.
  • There is currently no point specifying method parameters as they will not be populated. The plan is to ultimately have request parameters bound to method parameters like ?name=John => public profile(name: string) { this.request.query.name === name; }
  • Probably a bunch of other things...