GithubHelp home page GithubHelp logo

isabella232 / resourcer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from koajs/resourcer

0.0 0.0 0.0 9 KB

A simple resource directory mounter for koa.

License: MIT License

Makefile 9.38% JavaScript 90.62%

resourcer's Introduction

resourcer

A simple resource directory mounter for koa.

Build Status Coverage Status npm

var koa = require('koa');
var resource = require('koa-resourcer');

var app = koa();
resource(app, join(__dirname, 'resources') [, callback])
app.listen();

The idea behind koa-resourcer is complete modularity within your koa app. Each directory can be a stand-alone koa application with all it's own tests, docs, dependencies, etc. koa-resourcer makes it easy to mount these apps if you follow a simple directory pattern.

Directory structure

Say we have the following directory structure:

resources/
|-- v1/users/
|   |-- config.json
|   `-- app.js
|-- v1/products/
|   |-- config.json
|   |-- makefile
|   |-- index.js
|   |-- lib/
|   |   `-- utils.js
|   `-- test/
|       `-- index.js
`-- v2/users/
    |-- config.json
    `-- fun.js

We have three independent apps here. v1/users/app.js, v1/products/index.js and v2/users/fun.js. We could use koa-mount directly and be done with it but that requires the parent application to know where all the apps are and the paths on which they should be mounted.

var koa = require('koa')
var mount = require('koa-mount')
var app = koa()

var v1users = require('./v1/users/app.js')
app.use(mount('v1/users/', v1users))

var v2users = require('./v2/users/fun.js')
app.use(mount('v2/users/', v2users))

var v1products = require('./v1/products/')
app.use(mount('v1/products/', v1products))

// and so on, and so on, for each of your sub apps....

app.listen()

With koa-resourcer you let your directory structure and some simple config files do the talking for you, keeping your parent application peacefully oblivious.

var koa = require('koa')
var join = require('path').join
var resource = require('koa-resourcer')
var app = koa()

resource(app, join(__dirname, 'resources'))

app.listen()

config.json

Each application directory must contain a config.json file. The config.json file is used to discover the location of the application source code. This information is stored in it's source property.

// resources/v1/users/config.json

{ "source": "app.js" }

By relying on the directory structure to determine route paths, koa-resourcer will automatically mount app.js on /v1/users/ for you. If this path isn't quite what you want, koa-resourcer also supports configuring your path names in the config.json files.

// resources/v1/users/config.json

{ "source": "app.js", "path": "user" }

By adding the path property to our configuration, koa-resourcer will now mount the app on /v1/user/.

Callbacks

If you pass a callback function as the third argument to koa-resourcer, it will be executed for each mounted application passing an object containing the path and application resource.

var koa = require('koa')
var join = require('path').join
var resource = require('koa-resourcer')
var app = koa()

resource(app, join(__dirname, 'resources'), function(o){
  console.log('mounted %s', o.path, o.resource)
})

app.listen()

Installation

npm install koa-resourcer --save

Development

running tests

  • make test runs tests
  • make test-cov runs tests + test coverage
  • make open-cov opens test coverage results in your browser

verbose logging

koa-resourcer supports the debug module for help during development. Enable verbose logging by setting your DEBUG env variable like so:

DEBUG=koa-resourcer* npm test
```

## Sponsored by

[Pebble Technology!](https://www.pebble.com)

## License

[MIT](https://github.com/koajs/resourcer/blob/master/LICENSE)

resourcer's People

Contributors

aheckmann avatar snyk-bot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.