Javascript Nodejs / Express - 启动我的应用程序:express.createServer() 已弃用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13499010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nodejs / Express - Launching my app: express.createServer() is deprecated
提问by germainelol
I downloaded a node app to test and play around with. I have googled around and found that Express is found to be a little outdated. Can someone help me to fix the implemented code?
我下载了一个节点应用程序来测试和使用。我用谷歌搜索了一下,发现 Express 有点过时了。有人可以帮我修复已实现的代码吗?
Here is the code
这是代码
/**
* 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);
And here is the error I receive once running the app via node app
这是我通过以下方式运行应用程序时收到的错误 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.
固定的。
I am now at the point where I register, and go to login using my new 'User' data and receive this error:
我现在正在注册,并使用我的新“用户”数据登录并收到此错误:
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)
I'm wondering if this is some form of syntax error too? Not sure what's gone wrong here as I thought the code all lined up tbh.
我想知道这是否也是某种形式的语法错误?不确定这里出了什么问题,因为我认为代码都排好了。
I am using the code from here: https://github.com/rockbot/crowdnotes
我正在使用这里的代码:https: //github.com/rockbot/crowdnotes
回答by deven98602
The solution is given in the error.
错误中给出了解决方案。
Warning: express.createServer() is deprecated, express applications no longer inherit from http.Server please use:
警告:express.createServer() 已弃用,express 应用程序不再从 http.Server 继承,请使用:
var express = require("express");
var app = express();
So you will have to just do this.
所以你将不得不这样做。
var express = require('express')
, http = require('http');
var app = express();
var server = http.createServer(app);
回答by mlibre
Its can done with this simple program:
它可以通过这个简单的程序完成:
var express = require('express');
var app = express();
app.get('/',
function(req,res)
{
res.send("express");
}
);
app.listen(3333);
it works fine.
它工作正常。
回答by Warren Reilly
Another potential solution to this is to install express 2.5.8 as a dependency.
另一个可能的解决方案是安装 express 2.5.8 作为依赖项。
Add to package.json:
添加到 package.json:
{
"name": "authentication"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"express": "2.5.8"
, "jade": ">= 0.26.1"
}
}
and then run
然后运行
npm install
回答by Kowsalya
var express = require('express');
var app = express();
app.listen(your_port_number);
With the newer release of express (express 4.x), you do not need to create server. app.listen internally does that. Refer https://expressjs.com/en/4x/api.html#app.listen
使用较新版本的 express(express 4.x),您不需要创建服务器。app.listen 内部就是这样做的。参考https://expressjs.com/en/4x/api.html#app.listen
回答by Muhammad Soliman
I am already using the following snippet and it is working fine:
我已经在使用以下代码片段并且它工作正常:
var express =require("express");
var http = require("http");
var app = express();
//app routers here
...
var httpServer = http.Server(app);
httpServer.listen({PORT}, function(err){
});

