Express middleware to initialize your mongoose models into the request object.
npm install kuali-mongoose-express
const express = require('express')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const mongooseExpress = require('kuali-mongoose-express')
mongoose.Promise = Promise
const userSchema = {
name: 'User',
schema: new Schema({
username: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
}
})
}
const connection = mongoose.connect('mongodb://localhost/my_app', {
useMongoClient: true
})
const app = express()
app.use(mongooseExpress(connection, [
userSchema
]))
app.use(bodyParser.json())
app.post('/api/users', (req, res, next) => {
const User = req.models.User
return User
.create({
username: req.body.username,
password: req.body.password
})
.then((user) => res.json({ _id: user._id, username: user.username }))
.catch(next)
})
Returns express midleware that adds your mongoose models to the request
connection
{mongoose.Connection} - Your mongoose connection objectschemas
{Object[]} - An array of objects, with each object having aname
property (the name you want to give to mongoose.Model(name)) and aschema
property (the mongoose Schema for your model).