Last active
May 21, 2021 09:08
-
-
Save camshaft/4688223 to your computer and use it in GitHub Desktop.
Superagent Middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Module dependencies | |
*/ | |
var request = require("superagent"); | |
/** | |
* Middleware to sign the request | |
*/ | |
var authorizeRequest = function(token) { | |
// This will be called on the request | |
return function authorizeRequest(req, next) { | |
req.headers.authorization = "Bearer "+token; | |
// This will be called on the response | |
next(null, function (res, nextRes) { | |
nextRes(); | |
}); | |
} | |
}; | |
/** | |
* Make the request | |
*/ | |
request | |
.get("/api") | |
.use(signrequest("this-is-a-secret")) | |
.end(function(err, res){ | |
console.log(res.body) | |
}); | |
.use(signrequest("this-is-a-secret"))
should have been this right?
.use(authorizeRequest("this-is-a-secret"))
here is the simpler way:
var authorizeRequest = function(token) {
// This will be called on the request
return function authorizeRequest(req) {
req.header.authorization = "Bearer " + token;
return req;
}
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how bout now?