Created
January 2, 2022 12:06
-
-
Save vexdy/c8270b01c50ccf01dd0dc79d6c3b20df to your computer and use it in GitHub Desktop.
Nodejs Sharex Basic Upload Rest API
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 { nanoid } = require('nanoid'), | |
express = require('express'), | |
multer = require('multer'), | |
fs = require('fs'), | |
app = express() | |
const uploads = multer({ dest: 'static/' }) | |
app | |
.use(express.static('static')) | |
.use(express.urlencoded({ extended: true })) | |
.use(express.json()); | |
app | |
.post('/', uploads.single('file'), (req, res) => { | |
let ext = (req.body && req.body['extension']) ? "." + req.body['extension'] : '.jpg', | |
name = nanoid(8); | |
try { | |
fs.renameSync('static/' + req.file.filename, 'static/' + name + ext); | |
res.json({ status: 'ok', url: "http://localhost:3000/" + name + ext }); | |
} catch (e) { | |
res.json({ status: 'error', error: true, message: '' }); | |
}; | |
}); | |
app | |
.listen(3000, () => { | |
console.log('Example app listening on port 3000!'); | |
}); | |
/* | |
SXCU Config: | |
{ | |
"Version": "13.6.1", | |
"DestinationType": "ImageUploader", | |
"RequestMethod": "POST", | |
"RequestURL": "http://localhost:3000", | |
"Body": "MultipartFormData", | |
"FileFormName": "file", | |
"URL": "$json:url$" | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment