博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
expressjs mp4_将我们的“轻松节点身份验证”系列升级到ExpressJS 4.0
阅读量:2508 次
发布时间:2019-05-11

本文共 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及其新的独立依赖性。

The previous tutorials have been updated for Express 4. This tutorial will be for those that have already implemented this code and want to upgrade.
先前的教程已针对Express 4进行了更新。本教程将针对已实现此代码并想要升级的用户。

这是一个很大的变化吗? 与任何升级一样,您必须注意已实现的功能以及升级后的功能。 就我们的目的而言,必须在设置应用程序的方式上为我们做出重大改变。 由于Express 4.0将许多中间件删除到了自己的程序包中,因此我们必须抓住它们并为我们的应用程序配置它们。 我们只会看我们的 (Is this a big change? As with any upgrade, you'll have to be careful of what features you've implemented and how they'll function after the upgrade. For our purposes, the big changes for us have to be done in how we set up our application. Since Express 4.0 removed a lot of its middleware into their own packages, we have to go grab those and configure them for our application. We'll only be looking at our)

package.json and our server.js files.

package.json和我们的server.js文件。

包依赖项package.json我们正在使用 (Package Dependencies package.json We are using)

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让我们逐步进行。 首先,我们需要调用我们的新程序包。 (Setting Up Our Application server.js Let's go step by step. First, we will need to call our new packages.)

// 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已弃用。

旧代码快车3 (Old Code Express 3)

// 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});...

新密码快递4 (New Code Express 4)

// 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

我们做完了! 通过这些快速更改,我们已经全面升级,可以开始利用Express 4的新功能,例如其 (We're Done! With those quick changes, we're fully upgraded and can start taking advantage of the new features of Express 4 like its)

. Edit #1: Added bodyParser.json() and bodyParser.urlencoded({ extended: true });. Thanks for the tip .

编辑#1 :添加了bodyParser.json()bodyParser.urlencoded({ extended: true }); 。 感谢小费 。

series. 系列的一部分。
  • Upgrading for ExpressJS 4.0

    为ExpressJS 4.0升级

翻译自:

expressjs mp4

转载地址:http://pnywd.baihongyu.com/

你可能感兴趣的文章
tagName与nodeName的区别
查看>>
Js中Prototype、__proto__、Constructor、Object、Function关系介绍
查看>>
【BZOJ1975】【SDOI2010】魔法猪学院(搜索,A*,贪心)
查看>>
【BZOJ1189】紧急疏散(二分答案,最大流)
查看>>
C# 绘图三种方式
查看>>
第十二天
查看>>
Beta 冲刺 (6/7)
查看>>
关于前端工程师
查看>>
也谈kendoUI的grid.
查看>>
linux——(7)了解shell
查看>>
centos添加开放端口
查看>>
React Native 开发笔记
查看>>
set 集合 深浅copy
查看>>
github使用记录
查看>>
DELETE语句总结
查看>>
MySQL常见错误码及说明
查看>>
关于inodes占用100%的问题及解决方法
查看>>
eCharts使用(二) 坐标系
查看>>
转换函数
查看>>
如何在jupyter中安装R
查看>>