nodejs + http + mongodb实现mvc webapi

本文是笔者工作之余为打发时间写下的,实用性并不高。仅供参考。

不说废话贴出几个重要的js代码快

1.mongodb数据交互

const MongoClient = require('mongodb').MongoClient;
const config = require('./mongoConfig');

const eum = {
    insert: function (db, client, data, reslove, reject) {
        db.collection(data.name).insertMany(data.data, function (err, res) {
            client.close();
            if (err) {
                reject(err);
            } else {
                reslove(res);
            }
        });
    },
    delete: function (db, client, data, reslove, reject) {
        db.collection(data.name).deleteMany(data.data, function (err, res) {
            client.close();
            if (err) {
                reject(err);
            } else {
                reslove(res);
            }
        })
    },
    update: function (db, client, data, reslove, reject) {
        const id = data.data.id;
        delete data.data.id;
        db.collection(data.name).updateOne({ '_id': id }, { $set: data.data }, function (err, res) {
            client.close();
            if (err) {
                reject(err);
            } else {
                reslove(res);
            }
        })
    },
    findall: function (db, client, data, reslove, reject) {
        db.collection(data.name).find(data.data).toArray(function (err, res) {
            client.close();
            if (err) {
                reject(err);
            } else {
                reslove(res);
            }
        })
    },
    find: function (db, client, data, reslove, reject) {
        data.rows = parseInt(data.rows);
        data.page = parseInt(data.page);
        db.collection(data.name).find(data.data).limit(data.rows).skip((data.page - 1) * data.rows).toArray(function (err, res) {
            if (err) {
                reject(err);
            } else {
                db.collection(data.name).countDocuments(data.data, function (counterr, count) {
                    client.close();
                    if (counterr) {
                        reject(counterr);
                    } else {
                        reslove({ total: count, rows: res });
                    }
                })
            }
        })
    },
    findOne: function (db, client, data, reslove, reject) {
        db.collection(data.name).findOne(data.data, function (err, res) {
            client.close();
            if (err) {
                reject(err);
            } else {
                reslove(res);
            }
        })
    },
    count: function (db, client, data) {
        db.collection(data.name).findOne(data.data, function (err, res) {
            if (err) throw err;
            client.close();
        })
    }
}

module.exports = {
    do: function (eumtype, data) {
        return new Promise((reslove, reject) => {
            MongoClient.connect(config.serverUrl, { useNewUrlParser: true }, function (err, client) {
                const db = client.db(config.dataBaseName);
                eum[eumtype](db, client, data, reslove, reject);
            });
        })
    }
}
module.exports = {
    serverUrl: 'mongodb://localhost:27017',
    dataBaseName: ''
}
const INSERT = 'insert';
const UPDATE = 'update';
const DELETE = 'delete';
const FIND = 'find';
const COUNT = 'count';
const FINDONE = 'findOne';
const FINDALL = 'findall';
module.exports = {
    INSERT, UPDATE, DELETE, FIND, COUNT, FINDONE, FINDALL
}

 

2.访问参数定位控制器以及action

var fs = require('fs');

async function mapping(colltrollName, actionName, dataParams) {
    var result;
    dataParams = {
        name: colltrollName,
        data: dataParams
    };
    await fs.readdirSync(__dirname + '/controllers').filter((f) => {
        return f.startsWith(colltrollName);
    }).forEach((f) => {
        let colltro = require(__dirname + '/controllers/' + f);
        if (colltro[actionName]) {
            result = {
                code: 0,
                data: colltro[actionName](dataParams)
            }
        } else {
            result = {
                code: 404,
                data: null
            }
        }
    })
    return result;
}


module.exports = {
    mapping: mapping
}

3.控制器

const client = require('../mongoHelper');
const eumType = require('../eumType');

module.exports = {
    async getMenus(data) {
        return await client.do(eumType.FINDALL, data);
    },
}

 

你可能感兴趣的:(javascript)