在我的函数“reqHandler”中,我收集表单数据并将其传递给我的http.request。为了链接请求,我声明了一个Promise和.then处理程序。问题是: 1.这写入控制台“Unhandled promise rejection(rejection id:2):TypeError:无法读取未定义的属性'url'” 2.似乎没有调用.then,因此没有进行API调用。
代码:
"use strict";
const http = require("http");
const qs = require("querystring");
const fs = require("fs");
const PORT = 3000;
let resObject = {};
let hash = "";
const options = {
hostname: "netology.tomilomark.ru",
path: "/api/v1/hash",
method: "post"
};
const reqHandler = (req, res) => {
return new Promise((resolve, reject) => {
if (req.url === "/") {
switch (req.method.toLowerCase()) {
case "get":
// Browse my form with "Name" and "Surname" inputs
fs.readFile("./logs/form.html", (err, file) => {
if (err) {
reject("Promise rejected");
return
}
res.writeHead(200, {'Content-Type': 'text/html','Content-Length':file.length});
res.write(file);
res.end();
});
break;
case "post":
// Collect form data and parse it using querystring
let body = "";
req.setEncoding("utf8");
req.on("data", (data) => {
body += data;
if (body.length > 1e6)
req.connection.destroy();
});
req.on("end", () => {
let post = qs.parse(body);
console.log(post);
options.headers = {
"Content-Type": "application/json",
"firstname": `${post.firstName}`
};
options.body = {
"lastname": `${post.lastName}`
};
// Resolve with "options" object that has headers and body
resolve(options);
});
break;
default:
badRequest(res);
}
} else {
notFound(res);
}
});
};
reqHandler()
.then((options) => {
http.request(options, (res) => {
let resString = "";
res.on("data", (data) => {
resString += data;
});
res.on("end", () => {
console.log(resString);
});
res.end();
});
})
.catch(err => {throw err});
let badRequest = (res) => {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end('Bad Request');
};
let notFound = (res) => {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end('Not Found');
};
const server = http.createServer();
server.on("error", (err) => console.error(err));
server.on("request", reqHandler);
server.on("listening", () => console.log(`Start HTTP on port ${PORT}`));
server.listen(PORT);
最终,我的承诺错了。然后呢? 任何帮助将不胜感激!
答案 0 :(得分:1)
好的我已经创建了一个测试环境,在这里你有一个源代码的抽象版本。 你的错误是将你的承诺包装在一个传递req和res参数的函数中,你必须在你的承诺中调用或拒绝,在几个地方已经忘记了。
此来源已经过测试!
const http = require('http');
const reqHandler = (req, res) => {
return new Promise((resolve, reject) => {
if (req.url === "/") {
switch (req.method.toLowerCase()) {
case "get":
console.log('get');
return resolve('get');
case "post":
console.log('post');
return resolve('post');
default:
return resolve('default');
}
} else {
return resolve('some thing else');
}
});
};
const myReqHandler = (req, res) => {
reqHandler(req, res).then(()=> {
console.log('then reached')
});
}
const PORT = 8089;
const server = http.createServer();
server.on("error", (err) => console.error(err));
server.on("request", myReqHandler);
server.on("listening", () => console.log(`Start HTTP on port ${PORT}`));
server.listen(PORT);