Comments (3)
@hamzahalvanaa Hello. Did you try using this npm package https://www.npmjs.com/package/multer ?
Can you attach to the comment with an example of a request what you try to send? And please note, I did not find a file in your repository where you try to require multer library.
from node-typescript-mongodb.
Hi, @ChechaValerii. Before, I am new to express and MongoDB. Using your template and find tutorials on how to storing a file to the database. Here is the example of how I storing file:
src/routes/index.ts
const FOLDER_PATH = 'uploads';
const upload = multer({ dest: `${FOLDER_PATH}`, fileFilter: UtilsProvider.imageFilter });
app.use('/v1/uploads', upload.single('image'), UploadRouter);
src/components/Upload/index.ts
const DB_NAME = 'my_server';
const COLLECTION_NAME = 'uploadmodel';
const UPLOAD_PATH = 'uploads';
const db = new Loki(`${UPLOAD_PATH}/${DB_NAME}`, { persistenceMethod: 'fs' });
export async function findAll(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
// const uploads: IUploadModel[] = await UploadService.findAll();
const col = await UtilsProvider.loadCollection(COLLECTION_NAME, db);
res.send(col.data);
// res.status(200).json(uploads);
} catch (error) {
next(new HttpError(error.message.status, error.message));
}
}
Could you show me the correct ways how to modeling and service file upload based on your template? Thank you.
from node-typescript-mongodb.
Hello, sure I already create upload controller and test
components/Upload
import { HttpError } from '../../config/error';
import { NextFunction, Request, Response } from 'express';
/**
* @export
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
* @returns {Promise < void >}
*/
export async function upload(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
console.log(req.file); // all file info here
} catch (error) {
next(new HttpError(error.message.status, error.message));
}
}
components/interface.ts
import { IUploadModel } from './model';
/**
* @export
* @interface IUploadService
*/
export interface IUploadService {
/**
* @returns {Promise<IUploadModel[]>}
* @memberof IUploadService
*/
upload(): Promise<IUploadModel[]>;
}
routes/index.ts
/**
* @description
* Forwards any requests to the /v1/uploads URI to our UploadRouter
* Also, check if user authenticated
* @constructs
*/
app.use('/v1/uploads', UploadRouter);
routes/UploadRouter.ts
import { Router } from 'express';
import * as multer from 'multer';
import { UploadComponent } from '../components';
const router: Router = Router();
const upload: any = multer({ dest: 'uploads/' });
router.post('/upload', upload.single('avatar'), UploadComponent.upload);
export default router;
Please note, need add multer to a specific route, no need add parent.
And request must be with method POST and multipart/form-data
from node-typescript-mongodb.
Related Issues (5)
- npm commands in package.json do not work HOT 2
- Heroku Deploy HOT 1
- password reset HOT 1
- Error in src\config\oauth\index.ts HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-typescript-mongodb.