我正在尝试为简单节点项目创建文档,并且只想生成3个文件的文档。
routes.js
controller.js
- 醇>
model.js
routes.js的简单代码
/**
Holds code to route the brand specific APIs to the right functions
*/ *
var express = require('express');
var router = express.Router();
const brandController = require('../controllers/brand');
const tokenAuth = require('../lib/tokenAuth');
const permission = require('../lib/checkAccess');
router.post('/addBrand',tokenAuth.EmployeeValidateToken,permission.attributeWriteAccess, brandController.addBrand);
router.get('/brandList', brandController.getBrands);
我正在routes.js中使用middelware
controller.js的代码
const brandModel = require('../models/brand');
var brandController = {};
/**
*This is a function for add brand information
* @param {object} req
* @param {object} res
*@returns {object}
*/
brandController.addBrand = (req, res) => {
if(req.body.name != undefined){
let name = req.body.name
req.body.sortNm = name.toLowerCase();
}
let storeData = new brandModel(req.body);
storeData.save((err, brand) => {
if(err || brand == null ) {
console.log("addbrand: brand could not be inserted:"+ brand);
console.log("err", err);
if (err){
return res.json(400,{"data" :err})
} else{
return res.json(200,{"data" :brand})
}
} else{
return res.json(200,{"data" :brand})
}
})
/**
* This is a function get all brand information based on condition
* @param {Object} req
* @param {Object} res
* @returns {object}
*/
brandController.getBrands = (req, res)=>{
let condition = {};
console.log("req.query.data",req.query.data)
console.log("page", req.query.page);
console.log("limit", req.query.limit);
var skipValue, limitValue;
if(req.query.page != undefined && req.query.page > 1){
console.log("if");
skipValue = 10 *(req.query.page -1);
limitValue = 10;
}
else{
console.log("else");
limitValue = req.query.page == 1? 10: 1000;
skipValue = 0;
}
if(req.query.limit != undefined){
limitValue = req.query.limit;
}
var dta = new RegExp('^'+req.query.data, "i");
console.log("limitValue==" + limitValue + "skipValue==" + skipValue);
condition = (req.query.data != undefined) ?{$and:[{'sortNm':{$regex:dta}},{'isActive':true}]}: {'isActive':true}
console.log("condition", condition);
brandModel.find(condition, (err, data) =>{
if(err || data == null ) {
console.log("getBrand: brand could not be fetched:"+ data);
console.log("error", err);
if (err){
return res.json(400,{"data" :err})
} else{
return res.json(400,{"data" :data})
}
} else{
// console.log("response data", data);
brandModel.count(condition, (err, count)=>{
console.log("count", count);
totalPages = Math.ceil((count) / 10);
totalPages = totalPages == 0 ? 1 : totalPages;
return res.json(200, {
data: data,
totalPages: totalPages
})
})
}
}).limit(limitValue).skip(skipValue).sort({'seq':-1})
module.exports = brandController;
model.js的代码
/**
* Model for the brand collection.
* It is used to perform any db operations on brand collection
* @module models/brand
*/
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var autoIncrement = require('mongoose-auto-increment');
var brandSchema = new Schema({
name : {type:String, required: true},
isActive: {type:Boolean, default:true, required: false},
seq: { type: Number},
sortNm : {type: String, required: false, trim: true}
})
module.exports = mongoose.model('Brand', brandSchema);