两坨臭粑粑 2020-12-24 16:28 采纳率: 0%
浏览 332

nodejs写的接口放在服务器上报405 Not Allowed,来帮帮孩子吧!

用nodejs写了个接口dome,在本地运行都是正常的,放到服务器就出问题了

nodejs代码

const Koa = require('koa')
const app = new Koa()
let Router = require('koa-router')//路由
const koaBody = require('koa-body');//接收参数
let cors = require('koa2-cors')//解决跨域

app.use(
  cors({
    origin: function(ctx) { //设置允许来自指定域名请求
      // return 'http://wjjl.cool'; //只允许http://localhost:8080这个域名的请求
      return '*';
      // return ctx.header.origin
    },
    // maxAge: 5, //指定本次预检请求的有效期,单位为秒。
    credentials: false, //是否允许发送Cookie
    allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], //设置所允许的HTTP请求方法
    allowHeaders: ['Content-Type', 'Authorization', 'Accept'], //设置服务器支持的所有头信息字段
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'] //设置获取其他自定义字段
  })
);
app.use(koaBody());

let router = new Router();

router.get('/api/list', (ctx, next) => {
    // ctx.set('Content-Type', 'application/json')
    ctx.body = {
    state:0,
    data:[1,2,3],
    msg:'okok  getget!'
  }
});

router.post('/api/data', (ctx, next) => {
  ctx.body = {
    state:0,
    data:[1,2,3,999999],
    msg:'okok  post!'
  }
});


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

app.listen(5050,()=>{
  console.log('服务启动成功 5050')
})

前端请求配置

// 表单请求
axios.formRequest = axios.create({
  baseUrl: 'http://api.wjjl.cool',
  headers: {
    'Content-Type': 'text/plain;application/x-www-form-urlencoded;charset=UTF-8'
  },
  transformRequest: [
    function(data = {}) {
      return qs.stringify(data, { arrayFormat: 'indices' });
    }
  ]
});

get可以,post就405了

 

  • 写回答

3条回答 默认 最新

  • cwkisasb 2020-12-24 17:52
    关注

    hello

    评论

报告相同问题?