koa2 post总是显示“方法不允许”?

时间:2018-09-02 06:35:48

标签: koa

//server.js
const Koa = require('koa')
const app = new Koa();
const bodyParser = require('koa-bodyparser');
app.use(bodyParser());   
const Router = require('koa-router');
const fs = require('fs');

const router = new Router();
const UserController = require('./server/controller/user.js');
const checkToken = require('./server/token/checkToken.js');
router.get('/user/login', async ctx => {
 ctx.body = JSON.parse(fs.readFileSync( './pass.json'));
 console.log(ctx.body);
 });

router.post('/signin', async (ctx, next) => {
  var
    name = ctx.request.body.name || '',
    password = ctx.request.body.password || '';
    console.log(`signin with name: ${name}, password: ${password}`);
  if (name === 'koa' && password === '12345') {
 ctx.response.body = `<h1>Welcome, ${name}!</h1>`;
  } else {
 ctx.response.body = `<h1>Login failed!</h1>
 <p><a href="/">Try again</a></p>`;
 }
 });

     app.use(router.routes()).use(router.allowedMethods());

app.listen(8090, () => {
   console.log('The server is running at http://localhost:' + 8090);
 });

koa:2.52
koa-bodyparse:4.21
koa-router:7.4

当我键入http://localhost:8090/user/login时可以获取Json数据,但键入http://localhost:8090/signin时总是显示405 Methods Not Allowed,(debian firefxo)show Request Method“ GET”,响应Allow:POST,Connection:“活着” 希望得到您的帮助。

1 个答案:

答案 0 :(得分:1)

我想您不应该使用chrome来进行发布,因为键入一些URL时,默认方法是GET not POST,您可以从NETwork中将其检出。尝试邮递员就可以了。对不起,我很抱歉英语,希望对XD有帮助