首页 文章
  • 0 votes
     answers
     views

    koa2模板字符串无法正常显示

    我正在安装外套2.0和节点8.0,但是当我测试以下代码时,它显示Hello World $ ,而不是Hello World 6/7/2017 . const Koa = require('koa'); const app = new Koa(); app.use(ctx => { const date = new Date(); ctx.body = 'Hello World ${date...
  • 0 votes
     answers
     views

    将koa v1迁移到v2

    我正在使用koa的一些模块,他们只有这个用koa v1而不是v2编写的文档 . 因为我之前从未使用过v1,所以我不知道如何在v2中编写这个 . app .use(body({ IncomingForm: form })) .use(function * () { console.log(this.body.user) // => test console....
  • 1 votes
     answers
     views

    如何使用async / await和promise响应?

    我正在使用带有 Nodejs 7 的 Koa2 框架和本机异步/等待函数 . 而且我正在尝试在promise解析后为结果渲染模板( koa-art-template module) . const app = new koa() const searcher = require('./src/searcher') app.use(async (ctx) => { const para...
  • 1 votes
     answers
     views

    Koa-router忽略了Mongoose的async / await并且总是返回404

    这是路线的代码 . 当我使用注释 Promise 时,它在正文中返回123 . 但是使用mongoose查询它会返回404状态 . 日志中的项目很好 . 但似乎路由器只是忽略等待并立即返回404 . 我究竟做错了什么? router.get('/:id', async (ctx, next) => { // var item = await Promise.resolve(123);...
  • 6 votes
     answers
     views

    REST API与koa2 . 多个路由器的公共前缀

    我有两个实体,用户和员工 . 所以我想在不同的 endpoints 都使用CRUD,但是它们都将安装在“api”下,所以我可以定义api_v1,api_v2等等 . endpoints 将是这样的: get api/users put api/users/12 delete api/users/12 get api/employees .... 我的两条路线都无法获得“api”前缀 . 无法使...
  • 2 votes
     answers
     views

    路由器未在KOA2中定义

    我有两个文件,其中一个是app.js,另一个是api.js.在我的第一个文件中: app.use(setHeader) app.use(api.routes()) app.use(api.allowedMethods()) 在api.js我有: import KoaRouter from 'koa-router'; const api = new Router(); //Validat...
  • 0 votes
     answers
     views

    Nodejs,koa-router,koa-views(twig)返回“Not found”

    所以我开始了我的新项目,我的一个朋友召集我抛弃PHP并尝试使用rethinkDB尝试Nodejs . 我安装了一切,没有路线,它的工作原理 . 但是一旦我添加了路由,我得到了: Not Found ,控制台上没有错误 . Packages : "dependencies": { "keygrip": "^1.0.1", &...
  • 0 votes
     answers
     views

    koa路由器不起作用,发送404

    如果我从客户端发送POST这样的 /image/cover 或 /image/sub/ ,路由器功能不会起作用,但我确实不知道 . 我从来没有这种情况它只是没有理由不起作用 . router import Router from 'koa-router' const router = new Router({ prefix: '/image' }) router.post('/cover', ...
  • 0 votes
     answers
     views

    koa-static转到下一个中间件

    我有一个koa 2服务器 . 以下代码是我的中间件: // parse body app.use( bodyParser() ) // serve static app.use( serve( path.join(__dirname, '/public') ) ) // routes app.use( routes ) // error middleware app.use( async c...
  • 0 votes
     answers
     views

    如何在KOA 2中编写异步中间件

    我想解决一个承诺,然后在Koa 2中渲染一个类似的视图 . async function render(ctx, next) { // wait for some async action to finish await new Promise((resolve) => { setTimeout(resolve, 5000) }) // then, send resp...
  • 0 votes
     answers
     views

    仅在Koa v2中的服务器启动时执行一次中间件

    我创建了这个中间件,当网站中的任何路由获得访问者的第一个命中时,它只执行一次: // pg-promise const db = require('./db/pgp').db; const pgp = require('./db/pgp').pgp; app.use(async (ctx, next) => { try { ctx.db = db; ctx.pgp = ...
  • 1 votes
     answers
     views

    koa:promise vs async等待中间件

    我正在尝试编写一个Koa中间件,如果 condition met ,请转到下一个middelware . 如果 condition unmet ,则短路流量 . 我找到了两种方法,使用promise或async / await . Method 1: Promise-based app.use(function(ctx, next){ // if condition met if...
  • 0 votes
     answers
     views

    出口路线在Koa

    导出我的路线时遇到一个奇怪的问题 . 出于某种原因,此代码适用于我: app.js import Koa from 'koa' import routes from './routes/index' const app = new Koa() app.use(routes) app.listen(3000, () => { console.log('Server listening...
  • 5 votes
     answers
     views

    为什么我们在使用koa路由器时等待下一步呢?

    我们为什么要做这个 router.get('/data', async (ctx, next) => { ctx.body = dummyjson.parse(data); await next(); }); router.get('/data/:x', async (ctx, next) => { const newData = dataRepeat.replace('...
  • 0 votes
     answers
     views

    koa2:如何组织路由器控制器逻辑(中间件与非中间件)?

    我有一个“控制器”中间件(连接到koa-router) . 我想知道组织我的"internal" app逻辑的最佳方法是什么(超出我的控制器,连接到koa-router router.post('/', createCtrl) 的中间件功能) . 第一案: "Everything is a middleware" . 我的控制器只是编写了逐步从初始请求...
  • 3 votes
     answers
     views

    附加对象后,Koa会话重置?

    我有一个控制器查找一个字符,然后用它做一些东西,控制器看起来像: router.post('/profile/characters', async ctx => { try { ctx.type = 'json'; let req = ctx.request; if (!('charname' in req.body) || !('ch...
  • 2 votes
     answers
     views

    Koa路由器在击中路线之前得到解析的params

    我正在使用koa2和koa-router以及sequelize . 我希望能够根据他们在数据库中的角色来控制用户访问,到目前为止它一直在工作 . 我做了自己的RBAC实现,但是我遇到了一些麻烦 . 如果用户没有访问权限,我需要在任何 endpoints 被命中之前退出执行,考虑到 endpoints 可以执行任何操作(如插入新项目等) . 这很有道理,我意识到我可能会使用Sequelize进行交易...
  • 0 votes
     answers
     views

    如何在Koa 2中处理无效的GET请求?

    下面是我设置的简单Koa服务器 . 但是,每次执行无效的GET请求时, server "hangs" (如Chrome中的网络资源标签中)都会指定 pending . server.js const app = new Koa(); const apiUrl = `http://${KOA_HOST}:${API_PORT}`; const proxy = httpProx...
  • 2 votes
     answers
     views

    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); ...
  • 0 votes
     answers
     views

    koa2 koa-router mysql继续返回'Not Found'

    Background 我正在使用koa2和一些中间件来构建一个基本的api框架 . 但是当我使用“ctx.body”在我的路由器中发送响应时,客户端总是收到“Not Found” My code ./app.js const Koa = require('koa'); const app = new Koa(); const config = require('./config'); //Mid...
  • 0 votes
     answers
     views

    将 Value 从中间件传递到Koa 2中

    我试图从我的Koa 2中间件获取var值,以显示在我的哈巴狗模板(或其他)中 . 例如在koa-sessions中我有: app.use(ctx => { // ignore favicon if (ctx.path === '/favicon.ico') return; let n = ctx.session.views || 0; ctx.session.views =...
  • 0 votes
     answers
     views

    使用Koa和路由协商程序进行错误处理

    我正在寻找一个使用库routing-controllers的广义错误处理的解决方案 . 我的自定义错误处理应该能够捕获来自 Controller 的任何被拒绝的 Promise ,从中构建响应主体并根据响应上的错误类型设置HTTP代码 . 我不喜欢Koa默认的错误处理,因为我想吐出我的自定义响应体(不是堆栈跟踪等) . 在example中描述的简单设置上,在应用程序初始化时传递 Controlle...
  • 0 votes
     answers
     views

    为什么'koa-static'中间件会一直返回404?

    我正在使用koa-static来尝试Koa . 但是当使用多级包含关系时,它会一直返回 404 (Body: Not Found) . 我不知道原因 . To reproduce , Windows 10 x64,Node v9.11.1 Koa v2.5.1,koa-compose v4.1.0,koa-static v4.0.3,koa-send v4.1.3 Directory: i...
  • -1 votes
     answers
     views

    React-router和服务器端渲染

    我想用react-router和Koa渲染服务器端 . 但是,如果我包含完整的节点构建,我只能使它工作 . 这是我的组件的二重奏,需要sass编译,图像处理等 . 如果我的前端应用程序是预编译的,并且从那里获得路由及其相关页面呈现为字符串,那将是很好的 . 现在我从routes.js文件导入路由,该文件导出以下内容并使用未编译的组件: export default ( <Route ...
  • 0 votes
     answers
     views

    如何在Koa中间件中传递参数?

    所以我在Koa中有这个功能,基本上检查用户是否可以访问特定路由 . exports.requireRole = async role => async (ctx, next) => { const { user } = ctx.state.user; try { const foundUser = await User.findById(user.id)...

热门问题