nodejs 使用app.use(express.bodyParser()); 出行異常----解決方案

來源:互聯網
上載者:User

nodejs 使用app.use(express.bodyParser()); 出行異常----解決方案

異常代碼:

\Workspaces\WebStormProject\imooc-project\imooc>node app.jsError: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.    at Function.Object.defineProperty.get (L:\Workspaces\WebStormProject\imooc-project\imooc\node_modules\express\lib\express.js:89:13)    at Object. (L:\Workspaces\WebStormProject\imooc-project\imooc\app.js:12:17)    at Module._compile (module.js:456:26)    at Object.Module._extensions..js (module.js:474:10)    at Module.load (module.js:356:32)    at Function.Module._load (module.js:312:12)    at Function.Module.runMain (module.js:497:10)    at startup (node.js:119:16)    at node.js:902:3
當前express版本:3.5.3

根據錯誤可以知道bodyparser已經不和 Express綁定在一起了,說明當前express不包含body-parser,執行如下命令即可:npm install body-parser

解決:

var app = require('express')();var bodyParser = require('body-parser');var multer = require('multer'); app.use(bodyParser.json()); // for parsing application/jsonapp.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencodedapp.use(multer()); // for parsing multipart/form-dataapp.post('/', function (req, res) {  console.log(req.body);  res.json(req.body);})

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.