我在下面编写了此onRequest方法
onRequest(request: Http.IncomingMessage, response: Http.ServerResponse): void {
response.writeHead(200, {"Content-Type": "text/plain"});
const tc = new TaxCalculator();
const tax = JSON.stringify(tc.calculate(request.amount, request.rate));
res.send(tax);
}
我用上述方法得到了错误 属性'amount'在类型'IncomingMessage'上不存在。
我的问题是我们如何获得通过URL传递的数量和费率参数?
答案 0 :(得分:0)
如果您使用的是ES6和Express,请尝试以下解构方法:
const { amount, rate } = url.parse(request.url, true).query;
使用代码:
var url = require('url');
onRequest(request: Http.IncomingMessage, response: Http.ServerResponse): void {
response.writeHead(200, {"Content-Type": "text/plain"});
const tc = new TaxCalculator();
const queryData = url.parse(request.url, true).query;
const { amount, rate } = queryData;
const tax = JSON.stringify(tc.calculate(amount, rate));
res.send(tax);
}