Using everyauth with restify

僤鯓⒐⒋嵵緔 提交于 2019-12-05 01:09:37

问题


I'm trying to use everyauth to handle authentication for a rest api created with restify. But can't find a starting point. I'd expect to be doing something like:

var restify = require('restify');  
var everyauth = require('everyauth');
var server = restify.createServer();
server.use(everyauth.middleware());

but restify does not accept the everyauth middleware.

How do I go about setting up restify and everyauth?


回答1:


The issue you are having is restify does not and current will not have a middleware layer.

The below is from the author of restify

I've thought about this quite a bit, and the thing that worries me here is signing up for compatibility with connect evermore. I don't have control or input over what they decide to do. This seems more in the vein of "if it works, great".

I'm going to close this out with a "won't fix" for now :\

https://github.com/mcavage/node-restify/issues/89

What you can do is use connect and add the restify server on top of that, then you can use connect to manage your middleware like everyauth.

Here is a great sample of this, I have it working great on my system as-is.

// Restify server config here
var server = restify.createServer({
  name: 'restify-test',
  version: '1.0.0',
});

// ...

// Connect config here
var connectApp = connect()
    .use(connect.logger())
    .use(connect.bodyParser())
    .use(connect.query())
    .use(connect.cookieParser())
    // And this is where the magic happens
    .use("/api", function (req, res) {
             server.server.emit('request', req, res);
         });

connectApp.listen(8080);

https://gist.github.com/2140974

Then you can add everyauth to connect as per the documents.

Hope that helps.



来源:https://stackoverflow.com/questions/13101259/using-everyauth-with-restify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!