-
Notifications
You must be signed in to change notification settings - Fork 21
6. Upload File
leonardo Rico edited this page Dec 3, 2017
·
4 revisions
install the following npm packages
npm install express-fileupload --save
Add in src/lib/express/index.js
//...
import fileUpload from 'express-fileupload';
//...
//...
app.use(fileUpload());
//...
From controller src/api/exampleUploadFile.js
import { result, error } from 'express-easy-helper';
import config from '../../config';
// Upload a file
export async function index(req, res) {
try {
if (req.files) {
//file with name 'example' in form
let file = req.files.example;
let path = `/assets/${new Date().getTime()}_${file.name}`;
await file.mv(`${config.base}${path}`);
result(res, 201, { message: `Uploaded file!`, path });
} else {
throw 'Not found files!';
}
} catch (err) {
error(res, err);
}
}
Create router in swagger src/api/swagger/exampleUploadFile.yaml
/api/file/upload:
x-swagger-router-controller: exampleUploadFile
# index
post:
operationId: index
tags:
- Examples
summary: Upload a file
description: Upload a file
consumes:
- multipart/form-data
parameters:
- in: formData
name: example
type: file
description: The file to upload.
responses:
201:
description: Success
500:
description: Error
Nodetomic Api Swagger