0

/image/cover如果我这样或从客户端发送 POST /image/sub/,路由器功能根本不起作用,所以它发送 404。它应该可以工作,但我真的不知道。我从来没有遇到过这种情况它只是无缘无故地不起作用。

路由器

import Router from 'koa-router'

const router = new Router({ prefix: '/image' })

router.post('/cover', async (ctx, next) => {
    let URLpath = ctx.request.body.files.cover.path
    ctx.body = { url: URLpath }
})

router.post('/sub', async (ctx, next) => {
    let URLpath = ctx.request.body.files.sub.path
    ctx.body = { url: URLpath }
})

export default router

日志

  <-- POST /image/cover
  --> POST /image/cover 404 33ms -

导入代码(更新) router/index.js 从 'koa-compose' 导入 compose

import accountRouter from './account'
import mypageRouter from './mypage'
import imageRouter from './image'
import postRouter from './post'

const routes = [
  accountRouter,
  mypageRouter,
  imageRouter,
  postRouter
]

export default () => compose([].concat(
  ...routes.map(r => [r.routes(), r.allowedMethods()])
))

应用程序.js

import Koa from 'koa'
import serve from 'koa-static'
import logger from 'koa-logger'
import views from 'koa-views'
import session from 'koa-session'
import passport from 'koa-passport'
import bodyParser from 'koa-body'
import path from 'path'

import routes from './routes'
import config from './config'
import form from './util/formidable'

import mongoose from 'mongoose'
mongoose.Promise = global.Promise

mongoose
   .connect(config.MONGODB_URI)
   .then(startApp).catch(::console.error)

function startApp() {
    const app = new Koa()
    const port = process.env.PORT || 3000
    const dist = __dirname + '/views/'
    console.log(form)
    const bpOption = { IncomingForm: form, multipart: true }

    app.keys = ['secret', 'key'];
    require('./util/passport')

    app
      .use(logger())
      .use(serve(dist))
      .use(session({}, app))
      .use(bodyParser(bpOption))
      .use(passport.initialize())
      .use(passport.session())
      .use(views(__dirname+'/views', { extension: 'pug'}))
      .use(routes())

    app.listen(port, () => console.log(`[!] Server is Running on ${port}`))
}
4

1 回答 1

0

你需要帮助功能:

// @help function record route table map

'use strict';

const fs = require('fs');
const resolve = require('path').resolve;

module.exports = function record(router, filename) {
  const routeTable = fs.createWriteStream(resolve(__dirname, filename));
  routeTable.write(`Update Date: ${new Date().toString()}\n\n`);
  for (let len = router.stack.length, i = 0; i < len; i += 1) {
    const route = router.stack[i];
    routeTable.write(`URL:${route.path}\t${route.methods.join('|')}\n`);
  }
  routeTable.end();
};

那么你可以通过

const record = require('./record');
record(routeApp, '.routerc')

您将获得.routerc包含以下内容的文件(路线图):

URL: /a/c/c POST
URL: /b/a/d GET

它可以帮助您解决问题。也许工作。祝你好运!

于 2017-07-18T00:13:39.283 回答