node.js - Nodejs Express 文本文件上传,没有 multer、busboy 或任何其他 npm
问题描述
我有一个多行文本文件,我想通过 http 帖子在我的 nodejs 应用程序中接收它。
我不想发送多部分表单数据,我不想使用额外的包,例如 multer 或 busboy 或 express-fileupload。只是绝对的基本文件上传。保留换行符。
const express = require('express')
const http = require('http')
const app = express()
app.post('/postTextFile', function(req, res, next) {
...
})
var server = http.createServer(app)
server.listen(5002,'localhost')
我这样发送:
curl -X POST -H "Content-Type: text/plain" --d @./test.txt http://localhost:5002/postTextFile
文件是:
ABC
DEF
GHI
解决方案
您可以直接读取数据。
您的节点服务器:
const express = require('express');
const http = require('http');
const app = express();
app.post('/postTextFile', function(req, res, next) {
let data = '';
req.on('data', function(chunk) { data += chunk; });
req.on('end', function() {
console.log(data);
res.sendStatus(200);
});
});
var server = http.createServer(app);
server.listen(5002, 'localhost');
您的请求(用于--data-binary
保留多行):
curl -X POST -H "Content-Type: text/plain" --data-binary "@./test.txt" http://localhost:5002/postTextFile
在服务器上,你得到:
ABC
DEF
GHI
推荐阅读
- python - Python 从选项请求中获取数据
- python - 存在正则表达式时,“SuperList”对象不删除元素
- javascript - 设置 const 时反应组件覆盖另一个组件
- google-api - 谷歌推送通知给 ParseError
- android - 什么是android中的ContextWrapper
- node.js - 当我在登录后尝试重定向时,我得到 404 not found from a url 我什至没有打电话
- installation - Jupyterlab Plotly 扩展安装:JupyterLab 构建失败
- docker - 将两个 dockerfile 合二为一
- c# - C# Json LinQ - 使用循环构建 Json 字符串
- python - 用 numpy 去除多项式朴素贝叶斯