Tag: koa2

如何用asynchronousvalidation更新mongoose模型(node / koa2)

从客户端,用户有一个表格填写他们可以更新他们的电子邮件或密码。 他们必须input当前密码才能validation请求。 之后,如果电子邮件地址正在被修改,那么电子邮件validation器应该运行以检查新的电子邮件地址是否已经被注册到另一个用户。 所以,在用户从客户端发送请求后,我得到以下信息: { userId: 'This is the ID of their entry in the database', currentPassword: 'Current password to check against one stored in database', newPassword: 'New password, will be blank if user has not requested a new password', email: 'Email address, will be the same if user has not requested a new email' […]

如何redirect页面的标题?

我想redirect页面来修改标题的职位。 但是我发现在头部redirect在技术上是不可能的。 如果我发送请求到带有头部的节点服务器,将页面redirect到发布修改页面,那么没有办法使用头部redirect到不使用多个路由器的页面(因为路由器function只是用于渲染帕格文件,它们不能同时完成)

使用护照和护照。validationAFTER路线申报

我正在处理的应用程序要求所有的validation代码都放在路由声明之后,而不是通过中间件将路由分隔到公共和私有路由中,并且调用passport.authenticate在login方法(代码A)的callback中,我需要加载所有的路线在configuration护照(代码B)之前。 我修改了这个例子: https : //github.com/rkusa/koa-passport-example来帮助解释我正在努力完成的事情。 上面的auth.js的精简版本如下: const passport = require('koa-passport') const fetchUser = (() => { // This is an example! Use password hashing in your const user = { id: 1, username: 'test', password: 'test' } return async function() { return user } })() passport.serializeUser(function(user, done) { console.log("serializing") done(null, user.id) }) passport.deserializeUser(async function(id, done) […]

Koa每次发送状态404

export async function getPlaces(ctx, next) { const { error, data } = await PlaceModel.getPlaces(ctx.query); console.log(error, data); if (error) { return ctx.throw(422, error); } ctx.body = data; } Koa每次发送404状态和空身,我做错了什么?

Nodejs,将bin文件作为BYTEA存储到pgsql(损坏的文件)

出于某种原因,我需要将一些文件(主要是图像或PDF)存储到我的数据库(PG 9.2.20)。 这些文件是由用户上传,当我下载他们,他们已经损坏。 我在使用nodejs。 我存储文件的列types是BYTEA。 这是我如何存储他们: const { files, fields } = await asyncBusboy(ctx.req); const fileName = files[0].filename; const mimeType = files[0].mimeType; const bufferedFile = fs.readFileSync(files[0].path, { encoding: 'hex' }); const fileData = `\\x${bufferedFile}`; //Just a basic insert into with knex.raw const fileId = await storageModel.create(fields.name, fields.description, fileName, mimeType, fileData, ctx.user); 这就是我如何检索我的文件: const file = […]

Node.js / Koa2服务器 – 请求后执行一个工作分钟

我有一个Koa2 / Node.js应用程序(使用async/await ),我想执行一个工作X分钟后请求,其中X是一个随机数分钟范围从20 – 100(我想用它来发送自动欢迎邮件给注册的用户,并使其看起来像是由我个人发送的)。 所以我可以使用setTimeout来做到这一点,设置200分钟的定时器是否合理? 当然,如果我的应用程序崩溃,电子邮件将不会被发送,但我会跟踪数据库中的所有注册,所以在极less数情况下,我会自己发送电子邮件。

Koa正在执行两次每个请求?

Koa是否有任何理由为每个请求执行两次 ? const Koa = require('koa') const app = new Koa() const index = async(ctx, next) => { console.log('Hello world!') await next() ctx.body = 'Hello world!' } app.use(index); app.listen(3000) 在我的terminal上,我得到: Hello world! Hello world! 有任何想法吗?

Koa2:如何编写中间件链?

所以在expression中,我们可以有一个中间件链,复制一个例子: middleware = function(req, res){ res.send('GET request to homepage'); }); app.get('/', middleware, function (req, res) { res.send('GET request to homepage'); }); 请问在koa2中写这个的等效方法是什么? 我正在考虑使用它的路线,每条路线我想有一个中间件来检查用户是否已经login。 谢谢 !

如何使用asynchronous/等待承诺答复?

我使用Koa2框架与Nodejs 7和本地asynchronous/等待function。 我试图在promiseparsing后为结果呈现模板( koa-art-template模块)。 const app = new koa() const searcher = require('./src/searcher') app.use(async (ctx) => { const params = ctx.request.query if (ctx.request.path === '/') { searcher.find(params).then((items) => { await ctx.render('main', { items }) }) } }) 我想等待searcher模块获取项目,但Koa给我错误 await ctx.render('main', { items }) ^^^ SyntaxError: Unexpected identifier 如果我将设置为searcher.find(params).then(…)等待,一个应用程序将工作,但不会等待项目。

构成koaasynchronous函数中间件的错误

我正在使用koa-compose将指南中build议的中间件放在一起。 我inputkoa-compose作为kompose 。 我有以下代码: const home = async function home (ctx,next) { if(ctx.path === '/') { ctx.body = 'Hello World!' } else { await next() } } const random = async function random (ctx,next) { console.log(ctx) if(ctx.path === '/random') { console.log('Inside random function') ctx.body = Math.floor(Math.random() * 10) } else { await next() } } […]