【发布时间】:2018-04-20 02:56:26
【问题描述】:
我正在尝试学习如何创建 facebook Bot。
I found this amazing article on Medium which illustrates how we can create a messenger bot
在本文中,作者告诉我们在 controllers/verification.js. 中创建一个 verification.js. 文件,并将以下代码粘贴到其中。
module.exports = (req, res) => {
const hubChallenge = req.query[‘hub.challenge’];
const hubMode = req.query[‘hub.mode’];
const verifyTokenMatches = (req.query[‘hub.verify_token’] === ‘crowdbotics’);
if (hubMode && verifyTokenMatches) {
res.status(200).send(hubChallenge);
} else {
res.status(403).end();
}
};
现在,在试图弄清楚这段代码的作用(她已经解释过)之前,我无法理解为什么她没有在这个 Node.Js 文件中包含任何依赖项(精确表达)?
[更新] 谁能详细解释一下上面的代码是做什么的?
由于这段代码看起来像 NodeJS 代码,她不应该添加类似的东西
var express = require("express");
var app = express();
然后做module.exports?
【问题讨论】:
-
那是该页面上实际代码的第一行
-
只有在需要时才需要导入模块。这段代码只是简单地导出一个函数,通过导入它可以在任何其他模块中使用。
-
@lucas 没找到你?
-
Once the installation is complete, go to your directory and create a file called index.js and start Express server listening to the port 3000 (you can take any you want). const express = require(‘express’); const bodyParser = require(‘body-parser’); const app = express(); -
@RahulPatel 他只是在导出一个匿名的 es6 箭头函数,这是完全合法的。可以导入为
import * as whateverYouNameIt from 'controllers/verification';或let func = require('controllers/verification');。看看arrow functions 和node.js module exports
标签: javascript node.js bots