UNPKG

2.36 kBMarkdownView Raw
1[![Build Status](https://travis-ci.org/Empeeric/opinion.png?branch=master "Build Status")](https://travis-ci.org/Empeeric/opinion)
2
3[![NPM](https://nodei.co/npm/opinion.png)](https://nodei.co/npm/opinion/)
4
5# koa opinions
6
7Originally forked from `koajs/common`
8
9
10## Installation
11
12```js
13$ npm install opinion
14```
15
16## default configuration
17
18First of all we have a builtin [routing mechanism](https://github.com/alexmingoia/koa-router)
19
20An extensive default middleware stack
21```js
22DEFAULT_MIDDLEWARE_STACK = {
23 NoKeepAlive: common.NoKeepAlive,
24 responseTime: common.responseTime,
25 logger: common.logger,
26 compress: common.compress,
27 conditionalGet: common.conditionalGet,
28 etag: common.etag,
29 statics: common.statics,
30 session: common.session,
31 csrf: common.csrf,
32 router: common.router
33};
34```
35The request `ctx` has been extended with a `send` method to send files, and a `render` method to render views using any [`consolidate`](https://github.com/visionmedia/consolidate.js) compatible render engine, or plain `html` files.
36
37And as a extra bonus, `socket.io` is builtin and can be enabled by configuration flag.
38
39# Usage
40
41```js
42"use strict";
43var opinion = require('opinion');
44
45
46var app = opinion({
47 middlewareOrder: opinion.DEFAULT_MIDDLEWARE_STACK, // this can be manipulated
48 // here are some configurations, both general, and middleware specific (by name)
49 keys: ['78fd9fe83f2af46f2a8b567154db8d2a'],
50 statics: 'assets',
51 render: ['views', 'dust'],
52 socketio: { clientPath: '/js/socket.io.js' }
53});
54
55
56// simple route
57app.get('/',
58 function* () {
59 yield this.render('hello-world');
60 }
61);
62
63
64// a CORS enabled proxy to `gist.github.com`
65app.get('/snippet/cors/:user/:id', function* () {
66 this.set('Access-Control-Allow-Origin', '*');
67 this.set('Access-Control-Allow-Methods', 'GET');
68 this.set('Access-Control-Allow-Headers', 'Content-Type');
69 this.type = 'application/javascript';
70 this.body = require('request')('https://gist.github.com/' + this.params.user + '/' + this.params.id + '/raw');
71});
72
73
74app.listen(prosess.env.PORT || 8080, function () {
75 console.log("Server listening on %s", this._connectionKey);
76});
77
78
79// websocket push example
80setInterval(function () {
81 app.webSockets.emit('gaga', JSON.stringify(process.memoryUsage()))
82}, 3000);
83```
84
85
86## License
87
88 MIT
89