use node as the middle layer request server, please spray gently. The project is as follows:
- node version 8.11.2, using koa2 version 2.5.1
- because the node server is needed to request the backend api, the request module is used
- uses the koa2-cors package (2.0.5) for cross-domain processing
- uses koa-router (7.1.1)
// login
const request = require("request")
router.post("/login", async (ctx, next) => {
ctx.body = { //
code: 200,
msg: "success"
}
})
//
router.post("/login", async (ctx, next) => {
//
// api
request({
url: "http://xx.55.41.71:9090/tokens",
method: "POST",
json: true,
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
"userName": "xxxxxx",
"password": "xxxxxx"
})
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) //
ctx.body = body // request-promise-native
}
})
})
for more information about the above, please refer to the link
.problem description
//requestrequest-promise-native
// const request = require("request") // request
// request-promise-nativenpminstall requese --save
// demorequire("requese") npm install requese --save
const request = require("request-promise-native")
//
let res = await request({
url: "http://120.55.41.71:9090/tokens",
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
"userName": ctx.request.body.name,
"password": ctx.request.body.password
})
})
ctx.body = res
node reports an error as follows
because the time is relatively late, I am not sure that the error in the backend has caused my error (because I just found a problem with the postman call but I am not sure), so I feel that my node has made a mistake.
I want a big god to say what the mistake is? It is best to give a demo example of request-promise-native, look at the official documents of github, and have little knowledge of it in English. Look at the official request-promise (it seems to need the support of another plug-in), request-promise-native (native implementation, can not be used) request-promise-any (it seems to need a support, too). Which of these works well?