I have spent a day trying to figure this out. Given my js knowledge is dated I end up sorting out a moshpit of 'new' technologies where every doc and author expects you to know these things. It all creates one big brainfog. So I ask if anyone can explain how i enable require and Request in the code Im trying out.
To be specific, I have a working php API on my server using Hawk HTTP authentication. I am trying out the example javascript from the Github repo.
https://github.com/hueniverse/hawk
Client Code:
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)'));
});
Error
ReferenceError: require is not defined
I can relate to that this would cause an error when require is not part of plain javascript. What will i need to understand/learn/implement to make this work? Note that this is all on the client side.