Heim > Fragen und Antworten > Hauptteil
我也不说啥了,直接上代码吧!
var express = require('express'),
//session = require('express-session'),
mongoose = require('mongoose'),
router = require('./lib/router'),
bodyParser = require('body-parser'),
multer = require('multer'),
User = require('./lib/models/user'),
app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // for parsing application/json
app.use(multer);
app.use(express.static(__dirname + '/'));
//app.use(bodyParser.json());
mongoose.connect('mongodb://localhost:27017/tfdb');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.on("open", function () {
console.log("------数据库连接成功!------");
});
router(app);
//User.create({uid:123,name:'test','phone':'12313',password:'12312321'},function (err,data) {
// if(err)
// console.log(err);
//
//
//});
console.log('you have logged in successfully!');
app.listen(5000);
求大神解答
PHP中文网2017-04-17 15:21:02
app.use(multer); //此处错误
这样写会让multer把所有请求当做文件上传进行拦截,所以浏览器看不到响应
正确用法是绑定到特定请求,如下
let upload = multer({ dest: 'uploads/' })
app.post('/profile', upload.single('avatar'), function (req, res, next) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})