如何使用 NodeJs 代理多个文件上传?
How to proxy multiple file upload using NodeJs?
好的,所以我需要代理文件数组从我的快速节点应用程序上传到远程 PHP Api。
理想情况下,我会使用接近 Nginx 代理的东西,因为它与节点具有相同的模块。
如果没有,我会模拟表单重新发送。
你能帮忙找到最好的方法吗?
所以,我没有找到 execaly 如何代理请求本身,因为它会做 nginx,但至少我想出了如何将请求及其所有数据重定向到不同的源。
所以,这里我们使用 express-fileupload 从请求中获取文件数据,并使用 form-data 创建表单和发送数据。
import app from '@app';
import { authMw as checkJwtAndAddToRequest } from '@middleware/Auth';
import { Router } from 'express';
import fileupload, { UploadedFile } from "express-fileupload";
import FormData from 'form-data';
//add this MW to add files to you'r req
app.use(checkJwtAndAddToRequest)
app.use(fileupload());
app.post('/upload', (req, res) => {
const uploadableFiles: UploadedFile[] | UploadedFile | undefined = req.files?.formFieldName;
if(!uploadableFiles) {
throw Error('Pls add files to upload them');
}
//Transorm single file to same form as an array of files
const files: UploadedFile[] = Array.isArray(uploadableFiles) ? uploadableFiles : Array<UploadedFile>(uploadableFiles);
//create form
const form = new FormData();
//add files
files.forEach(
file => form.append('files[]', file.data.toString(), file.name)
)
//Submit
form.submit({
protocol: 'http:',
host: process.env.API_URL,
path: '/api-path-for/file/upload',
method: 'POST',
headers: {
//Add auth token if needed
authorization: `Bearer ${String(req.body.jwtToken)}`
}
}, (err, response) => {
if(err) {
//handle error
res.status(503).send('File server is currently unavailable');
}
//return remote response to original client
response.pipe(res)
});
});
好的,所以我需要代理文件数组从我的快速节点应用程序上传到远程 PHP Api。
理想情况下,我会使用接近 Nginx 代理的东西,因为它与节点具有相同的模块。
如果没有,我会模拟表单重新发送。
你能帮忙找到最好的方法吗?
所以,我没有找到 execaly 如何代理请求本身,因为它会做 nginx,但至少我想出了如何将请求及其所有数据重定向到不同的源。
所以,这里我们使用 express-fileupload 从请求中获取文件数据,并使用 form-data 创建表单和发送数据。
import app from '@app';
import { authMw as checkJwtAndAddToRequest } from '@middleware/Auth';
import { Router } from 'express';
import fileupload, { UploadedFile } from "express-fileupload";
import FormData from 'form-data';
//add this MW to add files to you'r req
app.use(checkJwtAndAddToRequest)
app.use(fileupload());
app.post('/upload', (req, res) => {
const uploadableFiles: UploadedFile[] | UploadedFile | undefined = req.files?.formFieldName;
if(!uploadableFiles) {
throw Error('Pls add files to upload them');
}
//Transorm single file to same form as an array of files
const files: UploadedFile[] = Array.isArray(uploadableFiles) ? uploadableFiles : Array<UploadedFile>(uploadableFiles);
//create form
const form = new FormData();
//add files
files.forEach(
file => form.append('files[]', file.data.toString(), file.name)
)
//Submit
form.submit({
protocol: 'http:',
host: process.env.API_URL,
path: '/api-path-for/file/upload',
method: 'POST',
headers: {
//Add auth token if needed
authorization: `Bearer ${String(req.body.jwtToken)}`
}
}, (err, response) => {
if(err) {
//handle error
res.status(503).send('File server is currently unavailable');
}
//return remote response to original client
response.pipe(res)
});
});