koa-scheme中间件报错~
发布于 6 年前 作者 gdut-zdc 4260 次浏览 最后一次编辑是 5 年前 来自 问答

app.js文件:

var app = require("koa")(),
      Jade = require("koa-jade"),
	  router = require("koa-router")(),
	  bodyParser = require("koa-bodyparser"),
	  scheme = require("koa-scheme");
	  jade = new Jade({
	  	viewPath: __dirname + '/template',
		debug: true,
		pretty: true,
		compileDebug: true,
		locals : {
			"name": "Jade"
		},
		noCache : true,
		app: app
    });

var staticServer = require("koa-static");
app.use(bodyParser());

//加载表态文件
app.use(staticServer(__dirname + '/common'));

app.use(router.routes());

router
	.get("/",function* (next){
		this.render("login.jade",{name:"Jade"},null,true);
	});
app.use(scheme(__dirname + '/scheme.js'),{debug:true});

package.json文件:

{
	"name": "koa",
	"version": "1.0.0",
	"private": true,
	"author": {
	"name": "Zander",
	  "email": "guyszdc@gmail.com"
  },
  "dependencies": {
	  "crypto": "0.0.3",
	  "install": "^0.5.4",
	  "koa": "^1.1.2",
	  "koa-bodyparser": "^2.0.1",
	  "koa-etag": "^2.1.1",
	  "koa-jade": "^2.1.0",
	  "koa-router": "^5.4.0",
	  "koa-scheme": "^2.2.0",
	  "koa-static": "^2.0.0",
	  "mysql-co": "0.0.4",
	  "validator": "^5.1.0"
  },
  "devDependencies": {},
  "scripts": {
	  "start": "node app.js"
  }
}

login页面发送的post请求:

 this.submitBtn.addEventListener("click",() => {
			  let xhr = new XMLHttpRequest();
			  xhr.open('POST',"http://localhost:3000/login");
			  xhr.onload = function(request) {
				  let response = request.currentTarget.response || request.target.responseText;
				  console.log(response);
			  };
			  xhr.send("username=" + that.userName.value + "&password=" + that.userPassword.value);
				//xhr.send({"username":that.userName.value,"password":that.userPassword.value});
			  return xhr;
		  })

scheme.js 文件:

/**
 * Created by guysz on 2016/3/4.
 */
var validator = require("validator");

module.exports = {
	  "/": {
		  "response": {
			  "status":200
		 }
	  },
	  "/login": {
	  	"request": {
			"method":"POST",
			"body":{
				"username":"[a-zA-Z]+",
				"password":"[a-zA-Z]+"
			}
		},
	"response": {
		"status":200
	}
    
};

页面中发送请求的时候,请求400,会返回 body.username : Not exist!

微信截图_20160304152903.png

11 回复

不格式没人看

@i5ting 已格式化。。

@gdut-zdc 学学markdown吧,编辑下源码,然后再看看

@i5ting 话说,你知道上面的问题吗?好奇怪,我是按照api来的。

xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 

试试

@i5ting 可以了!谢啦,真是这个问题~

@nswbmw 这两个中间件是基于koa-router的吗?

@nswbmw koa-router-scheme将校验逻辑写在了业务逻辑里面,感觉还不如koa-scheme呢

@nswbmw 呀,大神啊,原来这几个中间件是你写的啊,想问一下,如果使用了koa-router,koa-scheme是不是就不起作用了呢?

回到顶部