Created
May 22, 2020 20:04
-
-
Save arturi/10bd3638975215291dcb148e156124f0 to your computer and use it in GitHub Desktop.
A demo backend server for testing xhr uploads with Uppy client, created by @lakesare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const express = require('express'); | |
const multer = require('multer'); | |
const argv = require('minimist')(process.argv.slice(2)); | |
const port = argv.port || 9999; | |
const fileFolderName = argv.folder || 'files'; | |
const absolutePathToFileFolder = process.cwd() + '/' + fileFolderName; | |
const app = express(); | |
// Middleware - deal with CORS | |
app.use((request, response, next) => { | |
response.header('Access-Control-Allow-Origin', '*'); | |
response.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); | |
next(); | |
}); | |
// GET /files - serve uploaded files | |
app.use('/' + fileFolderName, express.static(absolutePathToFileFolder)); | |
// GET / - describe what eache endpoint does | |
app.get('/', (request, response) => { | |
response.send(` | |
<html> | |
<body> | |
<b>POST /upload</b> - XHR upload, that will return { url } of an uploaded file<br/> | |
<b>GET /files/fileName.jpg</b> - will return a file by its name from /files<br/> | |
<b>POST /error</b> - endpoint that will raise an error<br/> | |
<b>POST /timeout</b> - endpoint that will take 100 seconds to respond<br/><br/> | |
Files are kept in: ${absolutePathToFileFolder}<br/> | |
</body> | |
</html> | |
`); | |
}); | |
// POST /upload - uploads a file to the /files directory | |
const mutlerInstance = multer({ | |
storage: multer.diskStorage({ | |
destination: (request, file, cb) => { | |
cb(null, fileFolderName); | |
}, | |
filename: (request, file, cb) => { | |
const date = String(Date.now()); | |
const randomNumbers = parseInt(Math.random() * 1000000000000); | |
// .extension for better browser previews | |
const extension = path.extname(file.originalname); | |
const filename = date + randomNumbers + extension; | |
cb(null, filename); | |
} | |
}) | |
}); | |
app.post('/upload', mutlerInstance.any(), (request, response) => { | |
// => 'http://localhost:9999' | |
const currentUrl = request.protocol + '://' + request.get('host'); | |
// => 'files/1565118702577' | |
const filePath = request.files[0].path; | |
// => 'http://localhost:9999/files/1565118702577' | |
const url = currentUrl + '/' + filePath; | |
response.json({ url }); | |
}); | |
// POST /error - raises an error | |
app.post('/error', (request, response) => { | |
response.status(500).json({ message: 'You completely broke the entire site!' }) | |
}); | |
// POST /timeout - takes 100 seconds to respond | |
app.post('/timeout', (request, response) => { | |
setTimeout(() => { | |
response.end(); | |
}, 100 * 1000) | |
}); | |
app.listen(port, (error) => { | |
if (error) { | |
console.log(`Server start error: ${error}`); | |
} else { | |
console.log(`Server is listening on port: ${port}`); | |
// Create the /files folder if it doesn't already exist | |
if(!fs.existsSync(fileFolderName)) { | |
fs.mkdirSync(fileFolderName); | |
} | |
console.log('Serving files from folder:', absolutePathToFileFolder); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment