文件上传服务
Hi, I am building an app and want to use feathers server side only. I am planning to migrate my current server side implementation from another NodeJS framework (that I currently use) and use FeathersJS. The reason I'm doing it is because FetahersJS allows me to create multiple services that each one of them can use different database. One of the features that I noticed that FeathersJS don't solve in the Framework is files upload. I also noticed that there are some questions about it on GitHub as well as on Stackoverflow. After reading the questions, guides (like this one: https://GitHub.com/feathersjs/docs/blob/master/guides/advanced/file-uploading.md) I came up with a solution and I really need you to share your thoughts about it. Before I start describing the solution, here are some assumptions: - Use FeathersJS "native" capabilities as much as I can (hooks, service and more) - Use storage services to upload my files and not store them on my server file system or as BLOB on my database - The current implementation use Google Cloud storage to store the files - I don't save absolute URLs to files in my database because I want to be storage agnostic so if I decide to use AWS S3 instead of Google Cloud storage I just need to migrate all the content in the bucket from Google Cloud storage to S3 and point to a different service (or adapter) in my FeathersJS service My solution goes like this The service I started by generating a new Feathers service via the service CLI. the service name is files. Next, I npm installed multer which allows me to upload one or multiple files and handle the multipart/form-data header My files.service.js content looks like the following: // Initializes the files service on path /files const createModel = require('../../models/files.model') const hooks = require('./files.hooks') const createService = require('feathers-mongoose') const multer = require('multer') const multipartMiddleware = multer() module.exports = function (app) { const Model = createModel(app) const paginate = app.get('paginate') const options = { name: 'files', Model, paginate } // Initialize our service with any options it requires app.use('/files', multipartMiddleware.array('file', parseInt(process.env.FILES_SERVICE_MAX_ITEMS) || 1), function (req, res, next) { req.feathers.files = req.files; next(); }, createService(options) } // Get our initialized service so that we can register hooks and filters const service = app.service('files') service.hooks(hooks) } You can see that I use multer array because this solution allow users to upload multiple files. From the code above you can see that you can easily change the number of files via the FILES_SERVICE_MAX_ITEMS env variable (I am running feathers in Docker + Docker-compose locally and on Kuberentes remotely so it's very easy and …
内容来源: feathersjs/feathers