Lint JavaScript in Markdown
This commit is contained in:
parent
39f058e11c
commit
340dd4f1a3
7 changed files with 56 additions and 62 deletions
28
Readme.md
28
Readme.md
|
@ -50,32 +50,29 @@ Koa is an middleware framework, it can take 3 different kind function as middlew
|
|||
Here we write an logger middleware with different function.
|
||||
|
||||
### Common function
|
||||
```js
|
||||
|
||||
```js
|
||||
// Middleware normally take two parameters (ctx, next), ctx is the context for one request,
|
||||
// next is an function that is invoked to execute the downstream middleware. It returns a Promise with a then function for running code after completion.
|
||||
|
||||
app.use((ctx, next) => {
|
||||
const start = new Date;
|
||||
const start = new Date();
|
||||
return next().then(() => {
|
||||
const ms = new Date - start;
|
||||
const ms = new Date() - start;
|
||||
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
|
||||
});
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
### ___async___ functions (Babel required)
|
||||
|
||||
```js
|
||||
|
||||
app.use(async (ctx, next) => {
|
||||
const start = new Date;
|
||||
const start = new Date();
|
||||
await next();
|
||||
const ms = new Date - start;
|
||||
const ms = new Date() - start;
|
||||
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
### GeneratorFunction
|
||||
|
@ -83,14 +80,12 @@ app.use(async (ctx, next) => {
|
|||
To use generator functions, you must use a wrapper such as [co](https://github.com/tj/co) that is no longer supplied with Koa.
|
||||
|
||||
```js
|
||||
|
||||
app.use(co.wrap(function *(ctx, next) {
|
||||
const start = new Date;
|
||||
const start = new Date();
|
||||
yield next();
|
||||
const ms = new Date - start;
|
||||
const ms = new Date() - start;
|
||||
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
|
||||
}));
|
||||
|
||||
```
|
||||
|
||||
### Old signature middleware (v1.x)
|
||||
|
@ -98,15 +93,14 @@ app.use(co.wrap(function *(ctx, next){
|
|||
If you want to use old signature or be compatible with old middleware, you must use [koa-convert](https://github.com/gyson/koa-convert) to convert legacy generator middleware to promise middleware.
|
||||
|
||||
```js
|
||||
const convert = require('koa-convert')
|
||||
const convert = require('koa-convert');
|
||||
|
||||
app.use(convert(function *(next) {
|
||||
const start = new Date;
|
||||
const start = new Date();
|
||||
yield next;
|
||||
const ms = new Date - start;
|
||||
const ms = new Date() - start;
|
||||
console.log(`${this.method} ${this.url} - ${ms}ms`);
|
||||
}));
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
@ -122,7 +116,7 @@ $ npm install babel-preset-stage-3 --save
|
|||
|
||||
```js
|
||||
// set babel in entry file
|
||||
require("babel-core/register")({
|
||||
require('babel-core/register')({
|
||||
presets: ['es2015-node5', 'stage-3']
|
||||
});
|
||||
```
|
||||
|
|
|
@ -81,24 +81,24 @@ const app = new Koa();
|
|||
// x-response-time
|
||||
|
||||
app.use(async function (ctx, next) {
|
||||
const start = new Date;
|
||||
const start = new Date();
|
||||
await next();
|
||||
const ms = new Date - start;
|
||||
const ms = new Date() - start;
|
||||
ctx.set('X-Response-Time', `${ms}ms`);
|
||||
});
|
||||
|
||||
// logger
|
||||
|
||||
app.use(async function (ctx, next) {
|
||||
const start = new Date;
|
||||
const start = new Date();
|
||||
await next();
|
||||
const ms = new Date - start;
|
||||
const ms = new Date() - start;
|
||||
console.log(`${ctx.method} ${ctx.url} - ${ms}`);
|
||||
});
|
||||
|
||||
// response
|
||||
|
||||
app.use((ctx) => {
|
||||
app.use(ctx => {
|
||||
ctx.body = 'Hello World';
|
||||
});
|
||||
|
||||
|
@ -198,17 +198,17 @@ app.context.db = db();
|
|||
To perform custom error-handling logic such as centralized logging you can add an "error" event listener:
|
||||
|
||||
```js
|
||||
app.on('error', function(err){
|
||||
log.error('server error', err);
|
||||
});
|
||||
app.on('error', err =>
|
||||
log.error('server error', err)
|
||||
);
|
||||
```
|
||||
|
||||
If an error is in the req/res cycle and it is _not_ possible to respond to the client, the `Context` instance is also passed:
|
||||
|
||||
```js
|
||||
app.on('error', function(err, ctx){
|
||||
log.error('server error', err, ctx);
|
||||
});
|
||||
app.on('error', (err, ctx) =>
|
||||
log.error('server error', err, ctx)
|
||||
);
|
||||
```
|
||||
|
||||
When an error occurs _and_ it is still possible to respond to the client, aka no data has been written to the socket, Koa will respond
|
||||
|
|
|
@ -96,7 +96,7 @@ this.request.href
|
|||
Get request `Content-Type` void of parameters such as "charset".
|
||||
|
||||
```js
|
||||
const ct = this.request.type;
|
||||
const ct = this.request.type
|
||||
// => "image/png"
|
||||
```
|
||||
|
||||
|
@ -130,7 +130,7 @@ this.request.charset
|
|||
setter does _not_ support nested objects.
|
||||
|
||||
```js
|
||||
this.query = { next: '/login' };
|
||||
this.query = { next: '/login' }
|
||||
```
|
||||
|
||||
### request.fresh
|
||||
|
|
|
@ -146,11 +146,11 @@ If `response.status` has not been set, Koa will automatically set the status to
|
|||
Here's an example of stream error handling without automatically destroying the stream:
|
||||
|
||||
```js
|
||||
const PassThrough = require('stream').PassThrough
|
||||
const PassThrough = require('stream').PassThrough;
|
||||
|
||||
app.use(function * (next) {
|
||||
this.body = someHTTPStream.on('error', this.onerror).pipe(PassThrough())
|
||||
})
|
||||
this.body = someHTTPStream.on('error', this.onerror).pipe(PassThrough());
|
||||
});
|
||||
```
|
||||
|
||||
#### Object
|
||||
|
@ -238,7 +238,7 @@ app.use(function *minifyHTML(next){
|
|||
|
||||
if (!this.response.is('html')) return;
|
||||
|
||||
const body = this.body;
|
||||
let body = this.body;
|
||||
if (!body || body.pipe) return;
|
||||
|
||||
if (Buffer.isBuffer(body)) body = body.toString();
|
||||
|
|
|
@ -13,9 +13,9 @@
|
|||
|
||||
```js
|
||||
async function responseTime(ctx, next) {
|
||||
const start = new Date;
|
||||
const start = new Date();
|
||||
await next();
|
||||
const ms = new Date - start;
|
||||
const ms = new Date() - start;
|
||||
ctx.set('X-Response-Time', `${ms}ms`);
|
||||
}
|
||||
|
||||
|
@ -87,7 +87,7 @@ function logger(format) {
|
|||
console.log(str);
|
||||
|
||||
await next();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
app.use(logger());
|
||||
|
@ -102,7 +102,7 @@ app.use(logger(':method :url'));
|
|||
function logger(format) {
|
||||
return async function logger(ctx, next) {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -137,7 +137,7 @@ async function pi(ctx, next) {
|
|||
}
|
||||
}
|
||||
|
||||
const all = compose([random, backwards, pi])
|
||||
const all = compose([random, backwards, pi]);
|
||||
|
||||
app.use(all);
|
||||
```
|
||||
|
@ -241,15 +241,15 @@ $ DEBUG=koa* node --harmony examples/simple
|
|||
|
||||
```js
|
||||
const path = require('path');
|
||||
const static = require('koa-static');
|
||||
const serve = require('koa-static');
|
||||
|
||||
const publicFiles = static(path.join(__dirname, 'public'));
|
||||
const publicFiles = serve(path.join(__dirname, 'public'));
|
||||
publicFiles._name = 'static /public';
|
||||
|
||||
app.use(publicFiles);
|
||||
```
|
||||
|
||||
Now, instead of just seeing "static" when debugging, you will see:
|
||||
Now, instead of just seeing "serve" when debugging, you will see:
|
||||
|
||||
```
|
||||
koa:application use static /public +0ms
|
||||
|
|
Loading…
Reference in a new issue