forked from ElemeFE/restc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,049 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
const express = require('express'); | ||
const app = express(); | ||
const restc = require('../..'); | ||
const express = require('express') | ||
const app = express() | ||
const restc = require('../..') | ||
|
||
app.use(restc.express()); | ||
app.use(restc.express()) | ||
|
||
app.get('/', (req, res) => { | ||
res.send({ message: 'Hello world!' }); | ||
}); | ||
res.send({ message: 'Hello world!' }) | ||
}) | ||
|
||
app.get('/binary', (req, res) => { | ||
res.set('Content-Type', 'application/octet-stream'); | ||
res.set('Content-Disposition', 'attachment; filename="hello.txt"'); | ||
res.send('Hello world!'); | ||
}); | ||
res.set('Content-Type', 'application/octet-stream') | ||
res.set('Content-Disposition', 'attachment; filename="hello.txt"') | ||
res.send('Hello world!') | ||
}) | ||
|
||
app.listen(3000); | ||
app.listen(3000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
const app = require('koa')(); | ||
const restc = require('../..'); | ||
const app = require('koa')() | ||
const restc = require('../..') | ||
|
||
app.use(restc.koa()); | ||
app.use(restc.koa()) | ||
|
||
app.use(function *(next) { | ||
this.body = { message: 'Hello world!' }; | ||
}); | ||
app.use(function * (next) { | ||
this.body = { message: 'Hello world!' } | ||
}) | ||
|
||
app.listen(3000); | ||
app.listen(3000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
const Koa = require('koa'); | ||
const restc = require('../..'); | ||
const app = new Koa(); | ||
const Koa = require('koa') | ||
const restc = require('../..') | ||
const app = new Koa() | ||
|
||
app.use(restc.koa2()); | ||
app.use(restc.koa2()) | ||
|
||
app.use((ctx, next) => { | ||
ctx.body = { message: 'Hello world!' }; | ||
return next(); | ||
}); | ||
ctx.body = { message: 'Hello world!' } | ||
return next() | ||
}) | ||
|
||
app.listen(3000); | ||
app.listen(3000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
const express = require('./lib/express'); | ||
const koa = require('./lib/koa'); | ||
const koa2 = require('./lib/koa2'); | ||
const express = require('./lib/express') | ||
const koa = require('./lib/koa') | ||
const koa2 = require('./lib/koa2') | ||
|
||
module.exports = { express, koa, koa2 }; | ||
module.exports = { express, koa, koa2 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
const restc = require('./restc'); | ||
const restc = require('./restc') | ||
|
||
module.exports = () => (req, res, next) => { | ||
res.vary('Accept'); | ||
if (req.accepts([ 'json', 'html' ]) !== 'html') return next(); | ||
res.vary('Accept') | ||
if (req.accepts([ 'json', 'html' ]) !== 'html') return next() | ||
restc | ||
.then(html => res.end(html)) | ||
.catch(error => { | ||
console.error('[restc]', 'an error occurred when acquiring restc:', error.message); | ||
next(); | ||
}); | ||
}; | ||
console.error('[restc]', 'an error occurred when acquiring restc:', error.message) | ||
next() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
const restc = require('./restc'); | ||
const restc = require('./restc') | ||
|
||
module.exports = () => function*(next) { | ||
this.set('Vary', 'Accept'); | ||
if (this.accepts([ 'json', 'html' ]) !== 'html') return yield next; | ||
module.exports = () => function * (next) { | ||
this.set('Vary', 'Accept') | ||
if (this.accepts([ 'json', 'html' ]) !== 'html') return yield next | ||
try { | ||
this.body = yield restc; | ||
this.body = yield restc | ||
} catch (error) { | ||
console.error('[restc]', 'an error occurred when acquiring restc:', error.message); | ||
yield next; | ||
console.error('[restc]', 'an error occurred when acquiring restc:', error.message) | ||
yield next | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
const restc = require('./restc'); | ||
const restc = require('./restc') | ||
|
||
module.exports = () => (ctx, next) => { | ||
ctx.set('Vary', 'Accept'); | ||
if (ctx.accepts([ 'json', 'html' ]) !== 'html') return next(); | ||
ctx.set('Vary', 'Accept') | ||
if (ctx.accepts([ 'json', 'html' ]) !== 'html') return next() | ||
return restc | ||
.then(html => ctx.body = html) | ||
.then(html => { | ||
ctx.body = html | ||
}) | ||
.catch(error => { | ||
console.error('[restc]', 'an error occurred when acquiring restc:', error.message); | ||
return next(); | ||
}); | ||
}; | ||
console.error('[restc]', 'an error occurred when acquiring restc:', error.message) | ||
return next() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
module.exports = new Promise((resolve, reject) => { | ||
fs.readFile(path.join(__dirname, '../faas/index.html'), (error, result) => { | ||
if (error) { | ||
reject(error); | ||
reject(error) | ||
} else { | ||
resolve(result + ''); | ||
resolve(result + '') | ||
} | ||
}); | ||
}); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ | |
"index.js", | ||
"index.d.ts" | ||
], | ||
"scripts": { | ||
"lint": "eslint --fix ." | ||
}, | ||
"faas": { | ||
"domain": "restc", | ||
"public": "faas", | ||
|
@@ -21,5 +24,13 @@ | |
"notice": [ | ||
"[email protected]" | ||
] | ||
}, | ||
"devDependencies": { | ||
"eslint": "^4.2.0", | ||
"eslint-config-standard": "^10.2.1", | ||
"eslint-plugin-import": "^2.7.0", | ||
"eslint-plugin-node": "^5.1.0", | ||
"eslint-plugin-promise": "^3.5.0", | ||
"eslint-plugin-standard": "^3.0.1" | ||
} | ||
} |
Oops, something went wrong.