-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (65 loc) · 1.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const fastify = require('fastify')
const fastifyMultipart = require('fastify-multipart')
const concat = require('concat-stream')
const fastifyReplyFrom = require('fastify-reply-from')
const FormData = require('form-data')
const express = require('express')
const multer = require('multer')
const uploadMw = multer()
/**
* TAGRET SERVER - express
*/
const target = express()
target.post(
'/',
uploadMw.single('file'),
(request, reply) => {
console.log('==================================')
console.log('Target - request received')
console.log('File - ', request.file)
console.log('==================================')
reply.send(`File received: ${request.file.originalname}`)
}
)
/**
* PROXY SERVER - fastify
*/
const proxy = fastify({ logger: true })
proxy.register(fastifyMultipart)
proxy.register(fastifyReplyFrom, {
base: 'http://localhost:3001/'
})
proxy.post('/', (request, reply) => {
function _handleFile(field, file, filename, encoding, mimetype) {
file.pipe(concat(function (buf) {
const form = new FormData()
form.append('file', buf, {
filename,
contentType: mimetype
})
request.body = form
}))
}
function _onEnd(err) {
// TODO: handle err
const newHeader = request.body.getHeaders()
reply.from('/', {
rewriteRequestHeaders: (originalReq, headers) => {
return newHeader
}
})
}
request.multipart(_handleFile, _onEnd)
})
// SERVER INIT
target.listen(3001, (err) => {
if (err) {
throw err
}
console.log('Target server is listening on 3001')
proxy.listen(3000, (err) => {
if (err) {
throw err
}
})
})