Created
October 7, 2020 12:21
-
-
Save Krabaton/7610adc2bbf613e5f6e8ee0022b3e815 to your computer and use it in GitHub Desktop.
Upload formidable
This file contains 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 createError = require('http-errors') | |
const express = require('express') | |
const path = require('path') | |
const fs = require('fs').promises | |
const formidable = require('formidable') | |
const app = express() | |
const uploadDir = path.join(process.cwd(), 'uploads') | |
const storeImage = path.join(process.cwd(), 'images') | |
const parseFrom = (form, req) => { | |
return new Promise((resolve, reject) => { | |
form.parse(req, (err, fields, files) => { | |
if (err) { | |
return reject(err) | |
} | |
resolve({ fields, files }) | |
}) | |
}) | |
} | |
app.post('/upload', async (req, res, next) => { | |
const form = formidable({ uploadDir, maxFileSize: 2 * 1024 * 1024 }) | |
const { fields, files } = await parseFrom(form, req) | |
const { path: temporaryName, name } = files.picture | |
const { description } = fields | |
const fileName = path.join(storeImage, name) | |
try { | |
await fs.rename(temporaryName, fileName) | |
} catch (err) { | |
await fs.unlink(temporaryName) | |
return next(err) | |
} | |
res.json({ description, message: 'Файл успешно загружен', status: 200 }) | |
}) | |
const isAccessible = (path) => { | |
return fs | |
.access(path) | |
.then(() => true) | |
.catch(() => false) | |
} | |
const createFolderIsNotExist = async (folder) => { | |
if (!(await isAccessible(folder))) { | |
await fs.mkdir(folder) | |
} | |
} | |
// catch 404 and forward to error handler | |
app.use(function (req, res, next) { | |
next(createError(404)) | |
}) | |
// error handler | |
app.use((err, req, res, next) => { | |
res.status(err.status || 500) | |
res.json({ message: err.message, status: err.status }) | |
}) | |
const PORT = process.env.PORT || 3000 | |
app.listen(PORT, async () => { | |
createFolderIsNotExist(uploadDir) | |
createFolderIsNotExist(storeImage) | |
console.log(`Server running. Use on port:${PORT}`) | |
}) |
This file contains 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
{ | |
"name": "upload-formidable", | |
"version": "1.0.0", | |
"description": "", | |
"main": "app.js", | |
"scripts": { | |
"start;dev": "nodemon app.js" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"express": "^4.17.1", | |
"formidable": "^1.2.2", | |
"http-errors": "^1.8.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment