node.js 参考错误:请求未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42351983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:01:19  来源:igfitidea点击:

ReferenceError: request is not defined

node.jsexpress

提问by Sylar

Im trying to replicate a facebook messenger botbut keep getting request is not defined.

我试图复制一个facebook Messenger bot,但不断得到request is not defined.

Same code as facebook:

与facebook相同的代码:

function callSendAPI(messageData) {
  request({
    uri: 'https://graph.facebook.com/v2.6/me/messages',
    qs: { access_token: PAGE_ACCESS_TOKEN },
    method: 'POST',
    json: messageData

  }, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var recipientId = body.recipient_id;
      var messageId = body.message_id;

      console.log("Successfully sent generic message with id %s to recipient %s", 
        messageId, recipientId);
    } else {
      console.error("Unable to send message.");
      console.error(response);
      console.error(error);
    }
  });  
}

My node server.jslooks like this:

我的节点server.js如下所示:

const express = require('express');
const bodyParser = require('body-parser');
//const request = express.request;
const PAGE_ACCESS_TOKEN = 'abc';

let app = express();

app.use(bodyParser.urlencoded({ extended: false }));

// parse application/json
app.use(bodyParser.json());

[...]

function sendTextMessage(recipientId, messageText) {
  var messageData = {
    recipient: {
      id: recipientId
    },
    message: {
      text: messageText
    }
  };

  callSendAPI(messageData);
}

function callSendAPI(messageData) {..}

[...]

Am I missing something with express? Thanks

我在快递中遗漏了什么吗?谢谢

采纳答案by hlfrmn

This example is making use of third-party Request module.

这个例子使用了第三方请求模块

You could also use the native requestlike so: require('http').request(), if you want to, but I would say, that the requestmodule is very common, and a good tool to use.

您也可以像这样使用本机请求require('http').request(),如果您愿意,但我想说的是,请求模块非常常见,并且是一个很好的工具。

EDITActually, I just noticed that you have it in your code, but it's commented out!After taking a longer peek, your commented out requestis pointing to express.request, which, if used like request()will throw an error, since it's not a function. So, you should really use the Request module, or adjust the code to use native http.request.

编辑实际上,我只是注意到你的代码中有它,但它被注释掉了!看了更长时间后,您注释掉的request是指向express.request,如果使用 likerequest()会抛出错误,因为它不是函数。因此,您应该真正使用 Request 模块,或者调整代码以使用 native http.request

回答by Ashwani Panwar

You have notinstalled requestmodule.

您还没有安装request模块。

First install it npm install --save requestand then includeit var request = require('request');

首先安装它npm install --save request,然后包含var request = require('request');