我刚刚开始使用node.js并试图部署Pusher的示例身份验证代码,但它失败了 这是代码 -
var express = require( 'express' );
var Pusher = require( 'pusher' );
var app = express( express.logger() );
app.use( express.bodyParser() );
var pusher = new Pusher( { appId: '<appid>', key:'<key>', secret:'<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 );
执行node messaging-server.js会抛出以下内容 -
user@ubuntu:~/Desktop/messaging_server$ node messaging-server.js
Error: Most middleware (like logger) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
at Function.Object.defineProperty.get (~/Desktop/messaging_server/node_modules/express/lib/express.js:89:13)
at Object.<anonymous> (~/Desktop/messaging_server/messaging-server.js:11:28)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
如何调试?
答案 0 :(得分:0)
您已安装快递4,但Pusher的文档正在使用旧版本的快递。让我们使用morgan
替换现已解散的express.logger()
:
var express = require( 'express' );
var Pusher = require( 'pusher' );
var logger = require('morgan');
var app = express( logger() );
app.use( express.bodyParser() );
var pusher = new Pusher( { appId: '<appid>', key:'<key>', secret:'<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 );
(不要忘记运行npm install morgan
)