在 Nodejs 中获取表单数据
Get form-data in Nodejs
我在 nodejs 上有一个应用程序,我已经收到 json 格式的请求(这里没有问题)。
当我想通过form-data上传视频时,问题就来了。这是我的一些代码:
Server.js
var express = require('express');
var bodyparser = require('body-parser');
var app = express();
app.use(bodyparser.urlencoded({extended: true}));
app.use(bodyparser.json());
require('./routes/routes')(app);
Routes.js
module.exports = function(app){
app.post('/save', ensureAuthenticated, activity.publish); //JSON REQUEST
app.post('/video', ensureAuthenticated, activity.video); //FORM-DATA REQUEST
}
我尝试使用 express-formidable(在 Server.js 中),但它破坏了我的“/save”路径。有人有想法吗?
我建议你使用 multer
https://github.com/expressjs/multer
示例:
var express = require('express')
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var app = express()
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
})
app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
// req.files is array of `photos` files
// req.body will contain the text fields, if there were any
})
var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) {
// req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
//
// e.g.
// req.files['avatar'][0] -> File
// req.files['gallery'] -> Array
//
// req.body will contain the text fields, if there were any
})
我在 nodejs 上有一个应用程序,我已经收到 json 格式的请求(这里没有问题)。
当我想通过form-data上传视频时,问题就来了。这是我的一些代码:
Server.js
var express = require('express');
var bodyparser = require('body-parser');
var app = express();
app.use(bodyparser.urlencoded({extended: true}));
app.use(bodyparser.json());
require('./routes/routes')(app);
Routes.js
module.exports = function(app){
app.post('/save', ensureAuthenticated, activity.publish); //JSON REQUEST
app.post('/video', ensureAuthenticated, activity.video); //FORM-DATA REQUEST
}
我尝试使用 express-formidable(在 Server.js 中),但它破坏了我的“/save”路径。有人有想法吗?
我建议你使用 multer
https://github.com/expressjs/multer
示例:
var express = require('express')
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var app = express()
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
})
app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
// req.files is array of `photos` files
// req.body will contain the text fields, if there were any
})
var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) {
// req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
//
// e.g.
// req.files['avatar'][0] -> File
// req.files['gallery'] -> Array
//
// req.body will contain the text fields, if there were any
})