对于node.js,Pusher auth示例失败

时间:2014-07-15 11:36:47

标签: node.js authentication pusher

pusher.com上node.js的身份验证代码示例不起作用。

var express = require( 'express' );
var Pusher = require( 'pusher' );

var app = express( express.logger() );
app.use( express.bodyParser() );

var pusher = new Pusher( { appId: APP_ID, key: APP_KEY, secret: APP_SECRET } );

app.post( '/pusher/auth', function( req, res ) {
  var socketId = req.body.socket_id;
  var channel = req.body.channel_name;
  var auth = pusher.auth( socketId, channel );
  res.send( auth );
} );

var port = process.env.PORT || 5000;
app.listen( port ); 

我收到以下错误。我该如何解决这个问题?

TypeError: Object #<Pusher> has no method 'auth'

1 个答案:

答案 0 :(得分:0)

啊,无论出于何种原因,他们必须将方法名称从 auth 更改为 认证

这是我的示例的固定版本。注意第4行 pusher.authenticate

app.post( '/pusher/auth', function( req, res ) {
  var socketId = req.body.socket_id;
  var channel = req.body.channel_name;
  var auth = pusher.authenticate( socketId, channel );
  res.send( auth );
} );

以下是我在 pusher.js 文件中看到的内容。 在本地,在我的node_modules中:

/** Returns a signature for given socket id, channel and socket data.
 *
 * @param {String} socketId socket id
 * @param {String} channel channel name
 * @param {Object} [data] additional socket data
 * @returns {String} authentication signature
 */
Pusher.prototype.authenticate = function(socketId, channel, data) {
  return auth.getSocketSignature(this.config.token, channel, socketId, data);
};

Pusher.js package.json

{
  "name": "pusher",
  "description": "Node.js client to interact with the Pusher REST API",
  "version": "1.0.0",
  "author": {
    "name": "Pusher",
    "email": "support@pusher.com"
  },