我得到“大多数中间件(例如bodyParser),必须在nodeJS中单独安装”

时间:2018-11-20 14:23:02

标签: node.js npm

尝试配置使用多个中间件(例如body-parser)的应用程序时,出现以下错误:

  

错误:大多数中间件(例如bodyParser)不再与之捆绑在一起   Express,必须单独安装。请参阅   https://github.com/senchalabs/connect#middleware

我分别安装了body-parser

npm i body-parser

我的代码NodeJS是:

// Server definitions

var express = require('express');
var cors = require('cors');
var bodyParser = require('body-parser');
const neo4j = require('neo4j-driver').v1;

var restify = require('restify');
var expressJwt = require('express-jwt');
var session = require('express-session');
var config = require('./config.json')
var app = express();

var router = express.Router();

var port = 3003;
app.use(restify.plugins.bodyParser());
app.use(express.bodyParser());
var bodyParser = require('body-parser'); 
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());
app.set('port', process.env.PORT || 3003);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(methodOverride());
app.use(session({ resave: true, saveUninitialized: true, secret: 'uwotm8' }));

// parse application/json
app.use(bodyParser.json());                        

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

// parse multipart/form-data
app.use(multer());

app.use(express.static(path.join(__dirname, 'public')));


app.use(bodyParser.json());

app.use(bodyParser.json({ type: 'application/vnd.api+json' }))

app.use(cors());

app.use(session({ secret: config.secret, resave: false, saveUninitialized: true }));

var multer = require('multer');
var path = require('path');
var app = express();
var mkdirp = require('mkdirp')
var port = 3003;

var myModule = require('./api-mine-server/api-mine-controller.js');

app.use(express.static(path.join(__dirname, 'uploads')));

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

var storage = multer.diskStorage({

    destination: function (req, file, cb) {
       //code
        });
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname);
    }

});
var upload = multer({ storage: storage });

app.post("/upload", upload.array("uploads[]", 12), function (req, res) {

    //code
});

// -----------------  File Uploader code  ends  -------------------------

app.listen(app.get('port'), function(){
    console.log('Express server on port ' + app.get('port'));
 });

var server = app.listen(port, function () {
    console.log("Listening on port %s...", port);
});

1 个答案:

答案 0 :(得分:0)

您必须从Express中删除行app.use(express.bodyParser());,因为它正在使用body-parser,并且会引发错误

  

大多数中间件(如bodyParser)不再与Express捆绑在一起   并且必须单独安装