Tag: koa.js

如何用koajs下载csv文件

我使用koajs作为nodejs的框架。 我尝试创buildCSV数据,并将其响应给客户端,但不工作 let fields = ['code', 'status']; let p = new Promise((resolve, reject) => { json2csv({data: data, fields: fields }, (err, response) => { if (err) { reject(err); } else { resolve(response); } }); }); return p.then(data => { let fileName = 'promotioncode-' + moment().unix(); ctx.response.attachment(fileName + '.csv'); ctx.response.type = 'application/ms-excel'; ctx.body = data; }) […]

如何使用ES6在Koa.js中创build中间件

我最近在使用Express的背景下,首次在Koa.JS中构build了一个应用程序。 我正在使用Babel在运行时编译代码,同时开发给我一些ES6 +代码。 我在Koa中定义了一个中间件,这个中间件是我想要使用的,并存储在ctx对象中,稍后在我的应用程序中使用。 我不知道哪里出了问题,因为我可以在Express中做类似的工作(虽然不在ES6代码中)。 这是我的中间件: const Config = require('../../Config'); import jwt from 'jsonwebtoken'; const JWTController = () => { return { async generateToken(tokenVars) { const secretKey = Config.auth.secret; const claims = { sub: tokenVars.userid, iss: Config.auth.issuer, account: tokenVars.accountId, permissions: '' }; let token = await jwt.sign(claims, secretKey); return token; }, async decodeClaims(token) { const […]