- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个快速演示服务器,可以回显客户端向其发送的内容。
它用于学习事件,客户端将使用获取 API 进行 POST,例如:
fetch('http://localhost:5000/', {
method: 'POST',
body: JSON.stringify({ lab: 'fetch', status: 'fun' }),
headers: messageHeaders
})
数据的 body
将是一个字符串、JSON 或 FormData
.
我对 express 不是很熟悉,但我想要一个处理所有三种 body
类型的路由。目前我不知道该怎么做。
我从 this SO post 开始,但解决方案不适用于我的情况 - 我不认为 res.format API 允许我访问请求的主体,它似乎也不适用于多部分表单数据。
相反,我默认使用 body-parser
和 multer
潜在中间件黑客配置中的包:
// if client POST body is a string, parse as text
app.post('/', bodyParser.text(), (req, res, next) => {
const contentType = req.get('content-type');
if (!contentType.includes('text/plain')) {
return next();
}
res.write(JSON.stringify(req.headers, null, 2))
res.write('\n\n')
res.write(req.body)
res.end()
});
// if client POST body is JSON, parse as JSON
app.post('/', bodyParser.json(), (req, res, next) => {
const contentType = req.get('content-type');
if (!contentType.includes('application/json')) {
return next();
}
res.write(JSON.stringify(req.headers, null, 2))
res.write('\n\n')
res.write(JSON.stringify(req.body, null, 2))
res.end()
});
// if client POST body is FormData, parse as form-data
app.post('/', upload.fields([]), (req, res, next) => {
const contentType = req.get('content-type');
if (!contentType.includes('multipart/form-data')) {
return next();
}
res.write(JSON.stringify(req.headers, null, 2))
res.write('\n\n')
res.write(JSON.stringify(req.body, null, 2))
res.end()
});
是否有更标准或“更好”的模式来实现此功能?也许我只需要写一个处理程序?
最佳答案
一个更简洁的解决方案可能是将中间件作为数组传递给特定的路由,它最终会被解析。
app.post('/notes', [urlencodedParser, multiPart.array("files", 5)], (req, res)=> {
//do what you want
console.log(req.files);// to get the files
console.log(req.body);// to get the body
})
不过,您可以使用 app.use(middleware:parser);
但请注意,使用此方法,将为应用程序中 express 处理的所有路由调用中间件函数。
所以我认为在需要这种动力的特定路线上处理它会更整洁,而且性能可能更好。
关于javascript - 在单个快速处理程序中处理 multipart/formdata、application/json 和 text/plain?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49784509/
我有以下代码,使用ajax上传文件,因为我不想使用表单操作。但我无法这样做。代码是: Select xml file to upload: ajax代码是: $('#upload_xml').on
我有 3 个图像或多图像数组,其总大小约为。 29mb 或以上。我尝试使用 Ajax 和表单数据将其发布在服务器端。但是当我尝试在 MVC 中使用 FormData 发布大数据时它显示错误,有什么解决
我想了解浏览器如何解释表单数据。我知道一个 http 请求包含 [Method][Header][URL][Params][Body] 我不知道如何将表单数据放在那里?它是被解释为参数(查询字符串)还
我正在做一个项目,我需要在提交之前更改 FormData。我无法更改元素值,我必须更改进入 POST 的实际 FormData。 我尝试更改 form.onsubmit 以更新值(适用于某些字段,但其
我正在尝试使用 FormData html5 api 设置多文件上传。问题是我无法删除 FormData 键上的数组索引。例如: if(editor.frmData){ editor.
我试图在提交表单时获取表单数据键值对对象,使用 new FormData()构造函数。但它总是返回空数据。 我已经尝试过event.persist()避免 react 事件池,但没有任何效果 expo
在 jQuery 代码中, var formData = new FormData($('#content-submit')[0]); formData.append("Title",
我正在使用 fetch API 从浏览器发送发布请求。这是我的代码: const headers = new Headers(); headers.append("Content-Type", "ap
你好,我有他的上传文件表格 $("#form").on('submit',(function(e) { e .preventDefault(); $.ajax({
我需要根据用户输入(从 html 表单发送)创建一个 formData,因为我需要修改图像图片(压缩/调整大小/裁剪),然后将新的 FormData 提交到服务器: $(document).r
我似乎对 FormData 为空有疑问。我正在尝试在单个 POST 请求中上传文件和 JSON。我尝试了各种各样的方法,但似乎没有任何效果。我想知道我是否在这里弄乱了一些基本的东西,但我似乎找不到任何
我需要在 FormData 中添加图像数组,但只有第一个图像通过。 我知道问题出在 JS 代码中,因为当我将表单直接发送到 php 页面时,它工作正常。 JavaScript var url = $(
我正在以非正常方式获取文件及其值。表单中没有实际输入。因此,我尝试将文件附加到 formData 以进行 ajax 提交。 每当我尝试使用以下方法提交表单时,我的文件都不会上传。因此,我附加文件的方式
formData 的参数之一可以包含对象吗? 如果我执行以下操作,该对象将在服务器端转换为字符串。 formData.append('first_name', 'takuya'); formD
我想将带有附加数据的图像从 React.js 发送到 Django 后端。当我使用 FormData() 创建表单数据时,它无法发送数组(因为它将其转换为字符串)。这是我的代码: let formDa
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 此问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-topic
我有一个 Web 应用程序,它使用 FormData 对象通过 jQuery.ajax() 上传。到目前为止,一切都很好。数据作为 multipart/form-data 发送,服务器解析它,没有问题
这个问题已经有答案了: FormData.get function is undefined (2 个回答) 已关闭 7 年前。 我正在尝试进行简单的 ajax 文件上传,但收到“未捕获的类型错误:f
我正在使用 Angular 5,这是 Angular 的新手,但我已经研究过很多 JavaScript 框架。我陷入了一个问题,下面是我现在面临的描述问题。 我已经创建了表单,我想在提交时发送到服务器
我正在关注How-to-submit-additional-form-data并有一个正在提交附加表单数据的工作文件上传,即。附加文本区域输入字段 description[],具有分块和多文件支持,无
我是一名优秀的程序员,十分优秀!