我正在使用express 4.0,我知道主体解析器已经从express核心中取出,我正在使用推荐的替换,但我正在获得

bodyParser:使用单独的json/urlencoded中间件server.js:15:12 对扩展解析显式指定"extended: true" node_modules/ Body-parser /index.js:74:29

我在哪里可以找到这些所谓的中间件?或者我不应该得到这个错误?

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);

当前回答

检查这个答案 条带webhook错误:没有找到匹配有效载荷的期望签名

// Use JSON parser for all non-webhook routes
app.use((req, res, next) => {
  if (req.originalUrl === '/webhook') {
    next();
  } else {
    express.json()(req, res, next);
  }
});

// Stripe requires the raw body to construct the event
app.post('/webhook', express.raw({type: 'application/json'}), (req, res) => {
  const sig = req.headers['stripe-signature'];

  let event;

  try {
    event = stripe.webhooks.constructEvent(req.body, sig, webhookSecret);
  } catch (err) {
    // On error, log and return the error message
    console.log(`❌ Error message: ${err.message}`);
    return res.status(400).send(`Webhook Error: ${err.message}`);
  }

  // Successfully constructed event
  console.log('✅ Success:', event.id);

  // Return a response to acknowledge receipt of the event
  res.json({received: true});
});

其他回答

如果你使用express > 4.16,你可以使用express.json()和express.urlencoded()

已经添加了express.json()和express.urlencoded()中间件,以提供开箱即用的请求体解析支持。这使用了下面的expressjs/body-parser模块,因此目前需要单独使用该模块的应用程序可以切换到内置解析器。

Source Express 4.16.0 -发布日期:2017-09-28

用这个,

const bodyParser  = require('body-parser');

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

就变成了,

const express = require('express');

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

这意味着使用bodyParser()构造函数已被弃用,截至2014-06-19。

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

现在需要分别调用这些方法

app.use(bodyParser.urlencoded());

app.use(bodyParser.json());

等等。

如果你仍然得到一个urlencoded警告,你需要使用

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

扩展配置对象键现在需要显式传递,因为它现在没有默认值。

如果您正在使用Express >= 4.16.0,则在Express .json()和Express .urlencoded()方法下重新添加了body解析器。

就连我也面临着同样的问题。下面我提到的更改解决了我的问题。

如果您使用的是Express 4.16+版本,那么

您可能在代码中添加了如下所示的一行:

app.use (bodyparser.json ());//使用body-parser包

现在你可以用以下语句替换上面的代码行:

app.use (express.json ());//用于解析JSON主体

因为express.json()中的代码是基于bodyparser.json()的,所以这不会在应用程序中引入任何破坏性的更改。

如果你的环境中也有以下代码:

app.use (bodyParser。urlencoded({扩展:真}));

你可以将上面的代码行替换为:

app.use (express.urlencoded ());//解析url编码的主体

如果你得到一个警告,说你仍然需要将extended传递给express.urlencoded(),那么,将上面的代码更新为:

app.use(表达。Urlencoded ({extended: true}));

最后提醒一句:

如果您使用的是Express 4.16+,则可能不需要在应用程序中安装附加的主体解析器包。有许多教程包含了体解析器的安装,因为它们的年代早于Express 4.16的发布。

在我的情况下,它是typescript + vs code错误地标记为已弃用:

但是如果你检查源代码:

/** @deprecated */
declare function bodyParser(
    options?: bodyParser.OptionsJson & bodyParser.OptionsText & bodyParser.OptionsUrlencoded,
): NextHandleFunction;

declare namespace bodyParser {

您可以看到,它应该是构造函数,而不是名称空间。 所以不是typescript就是vs code弄错了。 一切都很好,在这种情况下没有发生弃用(bodyParser as namespace=

想要零警告?像这样使用它:

// Express v4.16.0 and higher
// --------------------------
const express = require('express');

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

// For Express version less than 4.16.0
// ------------------------------------
const bodyParser = require('body-parser');

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

解释:扩展选项的默认值已弃用,这意味着您需要显式地传递true或false值。

注意,对于Express 4.16.0及更高版本:重新添加了正文解析器,以提供开箱即用的请求正文解析支持。