本文共 5402 字,大约阅读时间需要 18 分钟。
expressjs mp4
With ExpressJS 4.0 coming out, a lot of people have been asking how to upgrade this tutorial to using Express and all of its and . Let's go through and update our application so that we are using Express 4.0 and its new separated out dependencies.
随着ExpressJS 4.0的问世,许多人一直在问如何将本教程升级为使用Express及其所有和 。 让我们浏览并更新我们的应用程序,以便我们使用Express 4.0及其新的独立依赖性。
package.json
and our server.js
files.
package.json
和我们的server.js
文件。
four packages that were moved into their own repositories. These packages are: * express.logger (now ) * express.cookieParser (now ) * express.bodyParser (now ) * express.session (now ) Let's add those dependencies to our
四个软件包被移到了自己的存储库中。 这些软件包是:* express.logger(现在是 )* express.cookieParser(现在是 )* express.bodyParser(现在是 )* express.session(现在是 )让我们将这些依赖项添加到我们的
package.json
file. Also, we'll update our express dependency to version 4!
package.json
文件。 另外,我们将更新对版本4的明确依赖!
{ "name": "node-authentication", "main": "server.js", "dependencies" : { "express" : "~4.0.0", "ejs" : "~0.8.5", "mongoose" : "~3.8.1", "passport" : "~0.1.17", "passport-local" : "~0.1.6", "passport-facebook" : "~1.0.2", "passport-twitter" : "~1.0.2", "passport-google-oauth" : "~0.1.5", "connect-flash" : "~0.1.1", "bcrypt-nodejs" : "latest", "morgan": "~1.0.0", "body-parser": "~1.0.0", "cookie-parser": "~1.0.0", "express-session": "~1.0.0" }}
Now we can run:
现在我们可以运行:
$ npm install
to pull in our new dependencies. With our new packages, let's go use them in our
引入我们的新依赖关系。 使用我们的新软件包,让我们在我们的产品中使用它们
server.js
file.
server.js
文件。
// server.js// set up ======================================================================// get all the tools we needvar express = require('express');var app = express();var port = process.env.PORT || 8080;var mongoose = require('mongoose');var passport = require('passport');var flash = require('connect-flash');var morgan = require('morgan');var cookieParser = require('cookie-parser');var bodyParser = require('body-parser');var session = require('express-session');
With those new packages we will adjust our application to use them. We will also get rid of
使用这些新软件包,我们将调整应用程序以使用它们。 我们还将摆脱
app.configure()
since that has been deprecated in Express 4.
app.configure()
因为Express 4已弃用。
// server.js...app.configure(function() { // set up our express application app.use(express.logger('dev')); // log every request to the console app.use(express.cookieParser()); // read cookies (needed for auth) app.use(express.bodyParser()); // get information from html forms app.set('view engine', 'ejs'); // set up ejs for templating // required for passport app.use(express.session({ secret: 'ilovescotchscotchyscotchscotch' })); // session secret app.use(passport.initialize()); app.use(passport.session()); // persistent login sessions app.use(flash()); // use connect-flash for flash messages stored in session});...
// server.js... // set up our express application app.use(morgan('dev')); // log every request to the console app.use(cookieParser()); // read cookies (needed for auth) app.use(bodyParser.json()); // get information from html forms app.use(bodyParser.urlencoded({ extended: true })); app.set('view engine', 'ejs'); // set up ejs for templating // required for passport app.use(session({ secret: 'ilovescotchscotchyscotchscotch' })); // session secret app.use(passport.initialize()); app.use(passport.session()); // persistent login sessions app.use(flash()); // use connect-flash for flash messages stored in session...
We have removed
我们已删除
app.configure()
which is no longer supported by Express 4. Notice how we also called the new packages that we created like bodyParser
instead of express.bodyParser
.
Express 4不再支持app.configure()
请注意,我们还调用了我们创建的新程序包,如bodyParser
而不是express.bodyParser
。
. Edit #1: Added bodyParser.json()
and bodyParser.urlencoded({ extended: true });
. Thanks for the tip .
。 编辑#1 :添加了bodyParser.json()
和bodyParser.urlencoded({ extended: true });
。 感谢小费 。
翻译自:
expressjs mp4
转载地址:http://pnywd.baihongyu.com/