bodyParser已棄用快遞4

本文翻譯自:bodyParser is deprecated express 4

I am using express 4.0 and I'm aware that body parser has been taken out of the express core, I am using the recommended replacement, however I am getting 我正在使用快遞4.0,我知道身體解析器已被取出快遞核心,我使用推薦的替代品,但我得到了

body-parser deprecated bodyParser: use individual json/urlencoded middlewares server.js:15:12 body-parser deprecated urlencoded: explicitly specify "extended: true" for extended parsing node_modules/body-parser/index.js:74:29

Where do I find this supposed middlewares? 我在哪裏可以找到這個假設的中間件? or should I not be getting this error? 或者我不應該收到此錯誤?

var express     = require('express');
var server      = express();
var bodyParser  = require('body-parser');
var mongoose    = require('mongoose');
var passport    = require('./config/passport');
var routes      = require('./routes');

mongoose.connect('mongodb://localhost/myapp', function(err) {
    if(err) throw err;
});

server.set('view engine', 'jade');
server.set('views', __dirname + '/views');

server.use(bodyParser()); 
server.use(passport.initialize());

// Application Level Routes
routes(server, passport);

server.use(express.static(__dirname + '/public'));

server.listen(3000);

#1樓

參考:https://stackoom.com/question/1e5La/bodyParser已棄用快遞


#2樓

It means that using the bodyParser() constructor has been deprecated , as of 2014-06-19. 這意味着自2014-06-19起不再使用bodyParser() 構造函數

app.use(bodyParser()); //Now deprecated

You now need to call the methods separately 您現在需要單獨調用這些方法

app.use(bodyParser.urlencoded());

app.use(bodyParser.json());

And so on. 等等。

If you're still getting a warning with urlencoded you need to use 如果您仍然收到urlencoded警告,則需要使用

app.use(bodyParser.urlencoded({
  extended: true
}));

The extended config object key now needs to be explicitly passed, since it now has no default value. 現在需要顯式傳遞extended配置對象鍵,因爲它現在沒有默認值。

If you are using Express >= 4.16.0, body parser has been re-added under the methods express.json() and express.urlencoded() . 如果您使用Express> = 4.16.0,則在express.json()express.urlencoded()方法下重新添加了body解析器。


#3樓

Want zero warnings ? 想要零警告嗎? Use it like this: 像這樣使用它:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

Explanation : The default value of the extended option has been deprecated, meaning you need to explicitly pass true or false value. Explanation :不推薦使用extended選項的默認值,這意味着您需要顯式傳遞true或false值。


#4樓

In older versions of express, we had to use: 在舊版本的express中,我們不得不使用:

app.use(express.bodyparser()); 

because body-parser was a middleware between node and express. 因爲body-parser是node和express之間的中間件。 Now we have to use it like: 現在我們必須使用它:

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

#5樓

I found that while adding 我發現在添加時

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

helps, sometimes it's a matter of your querying that determines how express handles it. 幫助,有時這是你的查詢的問題,決定了如何處理它。

For instance, it could be that your parameters are passed in the URL rather than in the body 例如,您的參數可能是在URL中而不是在正文中傳遞的

In such a case, you need to capture both the body and url parameters and use whichever is available (with preference for the body parameters in the case below) 在這種情況下,您需要捕獲bodyurl參數並使用可用的任何一個(在下面的情況下優先選擇body參數)

app.route('/echo')
    .all((req,res)=>{
        let pars = (Object.keys(req.body).length > 0)?req.body:req.query;
        res.send(pars);
    });

#6樓

body-parser is a piece of express middleware that reads a form's input and stores it as a javascript object accessible through req.body 'body-parser' must be installed (via npm install --save body-parser ) For more info see: https://github.com/expressjs/body-parser body-parser是一段快速中間件,它讀取表單的輸入並將其存儲爲可通過req.body訪問的javascript對象。必須安裝body-parser'(通過npm install --save body-parser )有關詳細信息,請參閱: https://github.com/expressjs/body-parser

   var bodyParser = require('body-parser');
   app.use(bodyParser.json()); // support json encoded bodies
   app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

When extended is set to true, then deflated (compressed) bodies will be inflated; extended設置爲true時,放氣(壓縮)的物體將膨脹; when extended is set to false, deflated bodies are rejected. extended被設置爲false時,被放氣的身體被拒絕。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章