@ricdotnet/upfile
v1.0.0
Published
two simple ways to use: - plain - promise - on event
Downloads
8
Readme
How to use (only with expressjs)
two simple ways to use:
- plain
- promise
- on event
import { Request, Response, NextFunction } from 'express';
import { Upfile } from '@ricdotnet/upfile';
// plain
// this version will use call the next() function when we finish parsing the incoming body
function upload(req: Request, res: Response, next: NextFunction) {
const upfile = new Upfile('uploads path');
upfile.parseIncomingBody(req, res, next);
}
// event
// we have an 'uploaded' event that gets fired when we finish parsing the incoming body
function upload(req: Request, res: Response, next: NextFunction) {
const upfile = new Upfile('uploads path');
upfile.parseIncomingBody(req, res);
upfile.on('uploaded', () => next());
}
// promise
// this just allows us to await until we finish parsing the incoming body
async function upload(req: Request, res: Response, next: NextFunction) {
const upfile = new Upfile('uploads path');
await upfile.parseIncomingBody(req, res);
next();
}
route.post('/upload', upload, (req: Request, res: Response) => {
// data available on
console.log(req.files); // files
console.log(req.body); // other data
});