Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,25 @@ const upload = multer({
const app = require('express')();
const http = require('http').Server(app);

app.post('/upload', upload.single('photo'), (req, res, next) => {
res.json(req.file)
})
// HACK - react-native generates FormData with an invalid boundary that might contain '/'
// we need to wrap the boundary value with quotes to fix it
// TODO - remove this when react-native releases the fix
const re = /^multipart\/form-data.\s?boundary=['"]?(.*?)['"]?$/i;
app.post(
'/upload',
// <https://github.com/facebook/react-native/issues/7564#issuecomment-266323928>
(req, res, next) => {
const contentType = req.headers['content-type'];
const match = re.exec(contentType);
if (match && match.length === 2)
req.headers['content-type'] = 'multipart/form-data; boundary="' + match[1] + '"';
next();
},
upload.single('photo'),
(req, res, next) => {
res.json(req.file)
}
)

let port = process.env.PORT || 3000;
http.listen(port, () => {
Expand Down