npm.io
0.1.23 • Published 8 years ago

agua

Licence
ISC
Version
0.1.23
Deps
9
Vulns
0
Weekly
0
agua middleware framework for nodejs

Warning

This project is under development.
Come back later.

Installation

agua requires node v7.6.0 or higher for ES2015 and async function support

$ npm install agua

Usage

Application structure

├── package.json
├── app.js (optional)
├── agent.js (optional)
├── app
|   ├── router.js
│   ├── controller
│   │   └── home.js
|   ├── extend (optional)
│   |   ├── helper.js (optional)
│   |   ├── filter.js (optional)
│   |   ├── request.js (optional)
│   |   ├── response.js (optional)
│   |   ├── context.js (optional)
│   |   ├── application.js (optional)
│   |   └── agent.js (optional)
│   ├── service (optional)
│   ├── middleware (optional)
│   │   └── response_time.js
├── config
|   ├── config.default.js
│   ├── config.prod.js
|   ├── config.test.js (optional)
|   ├── config.local.js (optional)
│   └── plugin.default.js
app.js
//  app.js
const Agua = require('agua').Agua;

const app = new Agua({
  basedir: path.resolve(__dirname),
});

app.listen(3000);
app/router.js
//  app/router.js
module.exports = (app) => {
  //  app.get(name, path, 'controller.method');
  app.get('homeIndex', '/home', 'home.index');
};
app/controller/home.js
//  app/controller/home.js
module.exports = (app) => {
  return class HomeController extends app.Controller {
    async index() {
      this.ctx.body = {
        message: 'helloworld',
      };
    },
  };
};
app/middleware/response_time.js
//  app/middleware/response_time.js
module.exports = (options, app) => {
  return async (ctx, next) => {
    let startTime = new Date();
    await next();
    let endTime = new Date();
    ctx.body = {
      ...ctx.body,
      latency: (endTime - startTime).toString(),
    };
  };
};
config/config.default.js
//  config/config.default.js
module.exports = {
  middleware: ['response_time'],
  'response_time': {
    enable: true,
  },
};