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

llisonly

v0.4.8

Published

A simple MVC framework based on Express

Downloads

20

Readme

f# Advanced

A simple MVC framework based on Express

Install

npm install advanced -g

Create an application

# create demo
advanced new app
# install app
cd app
npm install
# start app
node server.js

Visit http://localhost:8586

Create a controller

Controllers are placed in app/controllers.

For example app/controllers/test.js:

var Controller = require('advanced').Controller;

module.exports = Controller.extend({
    index: function() {
        this.res.send('Hello World!');
    }
});

Visit http://localhost:8586/test

Router

Router inherits Router of Express. But there are some enhanced features.

Custom router

You can use a string to specify a controller and a method. i.e. controller@method

In app/routes.js

var Router = require('advanced').Router,
    router = Router();

// a string to specify a controller and a method. i.e. controller@method
router.get('/test/add/:id(\\d+)', 'test@addTest');

module.exports = router;

Specify routes with group. It likes router.use. The difference is that you don't need to create a router manually.

var Router = require('advanced').Router,
    router = Router();

router.group('/group', function(router) {
    router.get('/a', 'test@a'); // /group/a
    router.get('/b', 'test@b'); // /group/b
}

module.exports = router;

Default router

A simple router based on file of controller is supported.

If doesn't match any custom router, it will go here.

For example:

req.path = '/this/is/a/path'
  1. is exists /this/is/a/path/index.js@index
  2. is exists /this/is/a/path.js@index
  3. is exists /this/is/a.js@path
  4. is exists /this/is.js@a, this.req.params[0] = 'path'

Mock data

If set env = 'development' and isMock = true in config.js, it will load the middleware of mock. The request which created by invoking Cotroller::request method will be mocked.

For example. Assume that the request path is /test/api. If there is a json file whose path is /mock/test/api.json, the json data will be sent by reading the file.

Debug template data

Add debug=true to query string. When you want render a template, in development mode, it will output json data that will be rendered to the template.

For example: Visit http://127.0.0.1:8586/?debug=true will get

{
    "test": 1
}

Create http request in node

This method uses request module.

Call Controller::request to create a request in node. A promise will be returned. The arguments pass to the function like below.

The baseUrl is Utils.c('api.defaults') which is assigned to this._api

{
    dataKey1: '/path1',
    datakey2: '/path2'
}

The data returned likes below.

{
    dataKey1: {...},
    dataKey2: {...}
}

The response data will be assigned to the corresponding key totally. But you can filter the response data by overriding the _filerData method.

module.exports = Controller.extend({
    _filterData: function(data) {
        return data.data;
    },

    index: function() {
        this.request({
            dataKey1: '/path1',
            datakey2: '/path2'
        }).then(function(data) {
            this.render('index.swig', data);
        }.bind(this));
    }
})

You can pass data to destination when you create a request. Any options that can be passed to request module also can be passed to request method.

  1. qs object containing querystring values to be appended to the uri
  2. form object to be passed to destination like to submit a form.

For example:

module.exports = Controller.extend({
    index: function() {
        this.request({
            dataKey1: {
                uri: '/path1',
                qs: {
                    name: 'Javey'
                },
                form: {
                    password: '123'
                }
            },
            datakey2: '/path2'
        }).then(function(data) {
            this.render('index.swig', data);
        }.bind(this));
    }
})

Api Proxy

Utils.proxy(req, res, host)

You don't need to do anything, When you want to forward a request(req). The apiProxy middleware can do anything for you. It can forward a request which is created by AJAX to the host server transparently.

Config

Config file is placed in config/. If the filename starts with config, it will be loaded automatically.

Utils.c(key, [value])

  • @param key {String|Object}
  • @param value {*}
  • @return {*} The config you get or set.

Set config

Use Utils.c(key, value) to set config. The key can be a object, if you want set a block of config. If the value depends the other one. You can specify it like below:

var conf = {
    a: 'a',
    ab: '{a}b' // ab depends a
}
Utils.c(conf);

Get config

Use Utils.c(key) to get config. The key is a string.

Utils.c('ab'); // the value is 'ab'

License

MIT