2012-11-21 10 views
25

Tôi đã tải xuống ứng dụng nút để kiểm tra và phát xung quanh. Tôi đã googled xung quanh và thấy rằng Express được tìm thấy là một chút lỗi thời. Ai đó có thể giúp tôi sửa mã được triển khai không?Nodejs/Express - Khởi chạy ứng dụng của tôi: express.createServer() không được chấp nhận

Đây là mã

/** 
* Module dependencies. 
*/ 

// base dependencies for app 
var express = require('express') 
    , routes = require('./routes') 
    , DB = require('./accessDB').AccessDB 
    , passport = require('passport') 
    , mongoose = require('mongoose') 
    , mongoStore = require('connect-mongodb'); 

var app = module.exports = express.createServer(); 
global.app = app; 

var DB = require('./accessDB'); 
var conn = 'mongodb://localhost/CrowdNotes'; 
var db; 

// SocketIO Configuration 
//var io = require('socket.io').listen(app); 
// 
//io.sockets.on('connection', function(socket) { 
// socket.on('user note', function (note) { 
// console.log(note); 
// }); 
//}); 

// Configuration 
app.configure(function(){ 
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'jade'); 
    app.use(express.cookieParser()); 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
    app.use(require('stylus').middleware({ src: __dirname + '/public' })); 
    app.use(express.session({ 
    store: mongoStore(conn) 
    , secret: 'applecake' 
    }, function() { 
    app.use(app.router); 
    })); 
    app.use(passport.initialize()); 
    app.use(passport.session()); 
    app.use(express.static(__dirname + '/public')); 
}); 

db = new DB.startup(conn); 

app.configure('development', function(){ 
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
}); 

app.configure('production', function(){ 
    app.use(express.errorHandler()); 
}); 

// Routes 
require('./routes')(app); 

app.listen(3000); 
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); 

Và đây là lỗi tôi nhận được khi chạy ứng dụng thông qua node app

C:\CrowdNotes>node app 
Warning: express.createServer() is deprecated, express 
applications no longer inherit from http.Server, 
please use: 

    var express = require("express"); 
    var app = express(); 


C:\CrowdNotes\app.js:63 
console.log("Express server listening on port %d in %s mode", app.address().po 
                   ^
TypeError: Object function app(req, res){ app.handle(req, res); } has no method 
'address' 
    at Object.<anonymous> (C:\CrowdNotes\app.js:63:67) 
    at Module._compile (module.js:449:26) 
    at Object.Module._extensions..js (module.js:467:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 
    at Module.runMain (module.js:492:10) 
    at process.startup.processNextTick.process._tickCallback (node.js:244:9) 

C:\CrowdNotes> 

FIXED.

Tôi bây giờ tại điểm mà tôi đăng ký, và đi đến đăng nhập sử dụng dữ liệu của tôi mới 'Người dùng' và nhận lỗi này:

ReferenceError: C:\CrowdNotes\views\account.jade:6 
    4| div#header 
    5|  h2 CrowdNotes 
    > 6|  p Hi, #{currentUser.name.first}! 
    7| 
    8| - if (myEvent) 
    9|  p.center My Event: #{myEvent.name} 

currentUser is not defined 
    at eval (eval at <anonymous> (C:\CrowdNotes\node_modules\jade\lib\jade.js:176:8)) 
    at exports.compile (C:\CrowdNotes\node_modules\jade\lib\jade.js:181:12) 
    at Object.exports.render (C:\CrowdNotes\node_modules\jade\lib\jade.js:216:14) 
    at View.exports.renderFile [as engine] (C:\CrowdNotes\node_modules\jade\lib\jade.js:243:13) 
    at View.render (C:\CrowdNotes\node_modules\express\lib\view.js:75:8) 
    at Function.app.render (C:\CrowdNotes\node_modules\express\lib\application.js:500:10) 
    at ServerResponse.res.render (C:\CrowdNotes\node_modules\express\lib\response.js:716:7) 
    at module.exports.getAccount (C:\CrowdNotes\routes\index.js:47:11) 
    at Promise.module.exports.getMyEvent (C:\CrowdNotes\accessDB.js:54:7) 
    at Promise.addBack (C:\CrowdNotes\node_modules\mongoose\lib\promise.js:128:8) 

Tôi tự hỏi nếu đây là một số hình thức lỗi cú pháp quá ? Bạn không chắc chắn những gì sai ở đây như tôi nghĩ rằng tất cả các mã xếp hàng tbh.

Tôi đang sử dụng mã từ đây: https://github.com/rockbot/crowdnotes

+1

Vui lòng gửi một câu hỏi khác cho một vấn đề khác – Mustafa

Trả lời

59

Các giải pháp được đưa ra trong báo lỗi.

Cảnh báo: express.createServer() bị phản đối, thể hiện các ứng dụng không còn kế thừa từ http.Server xin vui lòng sử dụng:

var express = require("express"); 
    var app = express(); 

Vì vậy, bạn sẽ phải chỉ làm điều này.

var express = require('express') 
    , http = require('http'); 

var app = express(); 
var server = http.createServer(app); 
+0

Cảm ơn bạn đã trả lời. Tôi đã thêm lỗi mới vào bài đăng ở trên, bạn có biết tại sao điều này xảy ra không? – germainelol

+0

lỗi là do mẫu không thể tìm thấy nơi đối tượng currentUser được xác định – deven98602

+0

Thú vị, thật khó để theo dõi đường dẫn cần thiết khi sử dụng currentUser trong ví dụ này. Ví dụ, tôi không thể nhìn thấy nơi người dùng hiện tại sẽ tiếp theo đi về các tập tin. – germainelol

2

Một giải pháp tiềm năng khác là cài đặt nhanh 2.5.8 làm phụ thuộc.

Thêm vào package.json:

{ 
    "name": "authentication" 
    , "version": "0.0.1" 
    , "private": true 
    , "dependencies": { 
     "express": "2.5.8" 
    , "jade": ">= 0.26.1" 
    } 
} 

và sau đó chạy

npm install 
5

của nó có thể thực hiện với chương trình đơn giản này:

var express = require('express'); 
var app = express(); 
app.get('/', 
    function(req,res) 
    { 
     res.send("express"); 
    } 
); 
app.listen(3333); 

nó hoạt động tốt.

0

Tôi đã sử dụng đoạn mã sau đây và nó đang làm việc tốt:

var express =require("express"); 
var http = require("http"); 

var app = express(); 
//app routers here 
... 
var httpServer = http.Server(app); 

httpServer.listen({PORT}, function(err){ 

}); 
0
var express = require('express'); 
var app = express(); 
app.listen(port number); 

Với việc phát hành phiên bản mới hơn của nhanh (hiện 4.x), bạn không cần phải tạo ra máy chủ. app.listen nội bộ làm điều đó. Tham khảo https://expressjs.com/en/4x/api.html#app.listen