我花了一天的时间来解决这个问题。鉴于我的js知识已经过时,我最终要整理出一个“新”技术的妙处,让每个文档和作者期望你知道这些东西。这一切都创造了一个大的脑雾。所以我问,如果任何人都可以解释我如何在代码中启用require和Request请求。如何使用javascript要求?
具体来说,我在我的服务器上使用Hawk HTTP身份验证可以运行php API。我正在尝试从Github回购示例的JavaScript。
https://github.com/hueniverse/hawk
客户端代码:
const Request = require('request');
const Hawk = require('hawk');
// Client credentials
const credentials = {
id: 'dh37fgj492je',
key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
algorithm: 'sha256'
}
// Request options
const requestOptions = {
uri: 'http://example.com:8000/resource/1?b=1&a=2',
method: 'GET',
headers: {}
};
// Generate Authorization request header
const header = Hawk.client.header('http://example.com:8000/resource/1?b=1&a=2', 'GET', { credentials: credentials, ext: 'some-app-data' });
requestOptions.headers.Authorization = header.field;
// Send authenticated request
Request(requestOptions, function (error, response, body) {
// Authenticate the server's response
const isValid = Hawk.client.authenticate(response, credentials, header.artifacts, { payload: body });
// Output results
console.log(`${response.statusCode}: ${body}` + (isValid ? ' (valid)' : ' (invalid)'));
});
错误
ReferenceError: require is not defined
我能理解,这会导致需要当错误不是普通的JavaScript的一部分。我需要了解/学习/实施这项工作?请注意,这一切都在客户端。
您需要一个模块打包程序,如[webpack](https://webpack.github.io/)或[browserify](http://browserify.org/)。 – evolutionxbox
[Javascript require()函数给出ReferenceError:require没有定义]的可能重复(http://stackoverflow.com/questions/23603514/javascript-require-function-giving-referenceerror-require-is-not-defined) – evolutionxbox
require()不是你的标准JavaScript的一部分。在您的问题和标签的上下文中,require()被构建到Node.js中以加载模块。 –