我使用node.js v4.6.0以及express,request和body-parser的最新版本,但我收到的错误代码是我无法修复的,有什么想法吗?
这是我的代码:
var express = require('express');
var request = require('request');
var bodyparser = require('body-parser');
var app = express();
app.use(bodyparser.urlencoded({extended: true}))
var webhook = process.env.DISCORD_WEBHOOK;
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.post('/webhook', (req, res) =>{
request({
method: 'POST',
url: webhook,
json: {
"content": req.body.msg,
"username": "Potato"
}
});
res.redirect("/");
});
app.listen(80, () => {
console.log("Server Started!");
});
并收到错误消息:
Error: options.uri is a required argument
at Request.init (C:\Users\kingn\node_modules\request\request.js:233:31)
at new Request (C:\Users\kingn\node_modules\request\request.js:129:8)
at request (C:\Users\kingn\node_modules\request\index.js:55:10)
at C:\Users\kingn\index.js:15:5
at Layer.handle [as handle_request] (C:\Users\kingn\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\kingn\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\Users\kingn\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\kingn\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\kingn\node_modules\express\lib\router\index.js:277:22
at Function.process_params (C:\Users\kingn\node_modules\express\lib\router\index.js:330:12)
任何修复?
答案 0 :(得分:2)
您需要在提出请求时提供网址信息,请检查您的网络链接变量是否已分配正确的网址,即
var webhook = process.env.DISCORD_WEBHOOK; //应为webhook分配有效的网址,例如:' https://stackoverflow.com/'
试 的console.log(网络挂接);
找出您要提出请求的网址。
答案 1 :(得分:-1)
对象请求有参数uri not url。 https://github.com/request/request#multipartrelated
request({
method: 'POST',
uri: webhook,
json: {
"content": req.body.msg,
"username": "Potato"
}
});