如何在 Node.js 和 MongoDB 中将模式模型与文件上传相结合?

How to combine schema model with file upload in Node.js and MongoDB?

大家好,我正在构建一个 Web 应用程序来将员工信息存储在本地数据库中。我已经编写了必要的代码,以便客户可以通过表单输入所需的信息,例如姓名、电子邮件、phone 号码等,并将其保存到 MongoDB。现在我想添加一个文件上传功能,这样文件就可以和员工信息一起上传,比如保险单、护照复印件等。我正在努力寻找一种方法来实现这一点,因为很多在线方法都显示了 CRUD 和文件的实现单独上传。有没有经验丰富的人可以提出实施方法的建议?我是 Node 开发新手,不熟悉可用框架的大量选择。到目前为止,我有以下设置:

使用的框架: Express、Mongoose、Express-Handlebars(查看引擎),Bootstrap

employee.Model:

const mongoose = require('mongoose');
const mongoURI = 'mongodb://localhost:27017/testDB'
const conn = mongoose.createConnection(mongoURI, {useNewUrlParser: true});


Schema = mongoose.Schema;
var employeeSchema = new Schema({
    fullName: {
        type: String,
        required: true
    },
    email: {
        type: String
    },
    mobile: {
        type: String
    },
    city: {
        type: String
    }
    });

const Employee = conn.model('Employee', employeeSchema);
module.exports = Employee;

员工控制员:

const mongoose = require ('mongoose');
const express = require ('express');
var router = express.Router();
const Employee = require('../models/employee.model');

router.post('/', (req, res) => {
    insertRecord(req, res);
});

function insertRecord (req, res) {
var employee = new Employee();
employee.fullName = req.body.fullName;
employee.email = req.body.email;
employee.mobile = req.body.mobile;
employee.city = req.body.city;
employee.save((err, doc) =>{
    if (!err)
        res.redirect('employee/list');
    else {
        console.log('Error during record insertion: ' + err);
    }
});
}

把手视图:

<h3>{{viewTitle}}</h3>

<form action="/employee" method="POST" autocomplete="off">
    <div class="form-group">
        <label>Full Name</label>
        <input type="text" class="form-control" name="fullName" 
placeholder="Full Name">
    </div>
    <div class="form-group">
        <label>Email</label>
        <input type="text" class="form-control" name="email" 
placeholder="Email">
    </div>
    <div class="form-row">
        <div class="form-group col-md-6">
            <label>Mobile</label>
            <input type="text" class="form-control" name="mobile" 
placeholder="Mobile">
        </div>
        <div class="form-group col-md-6">
            <label>City</label>
            <input type="text" class="form-control" name="city" 
placeholder="City">
        </div>
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-info"><i class="fa fa- 
database"></i> Submit</button>
    </div>
</form>

上传的文件与其他文件一样是输入,它们只需要在 HTML 端具有特定的 enctype="multipart/form-data" 属性,并在服务器上进行适当的处​​理。

以及使用Multer的简单易懂的演示(实际上是基于他们自己的例子):

const upload = require('multer')({dest: 'uploads/'}); // npm i multer

const fileUploadMiddleware =  upload.fields({
  {name: 'resume', maxCount: 1},
  {name: 'insurance', maxCount: 4}
});
router.post('/', fileUploadMiddleware, (req, res) => {
  insertRecord(req, res);
});

function insertRecord(req, res) {
  ...
  console.log(req.files.resume[0].mimetype); // this is resume file type
  console.log(req.files.insurance[0].path); // this is first insurance file path
  console.log(req.body.fullName); // this still works
}