gpt4 book ai didi

node.js - 在 Nodejs 中获取表单数据

转载 作者:行者123 更新时间:2023-12-01 23:54:08 25 4
gpt4 key购买 nike

我在 Nodejs 上有一个应用程序,并且我已经收到 json 格式的请求(这里没有问题)。

当我想通过表单数据上传视频时,问题就出现了。这是我的一些代码:

服务器.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);

路由.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
})

关于node.js - 在 Nodejs 中获取表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52191614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com