@narmy/core
v0.8.10
Published
a crud api for express and mongodb with mongoose
Downloads
119
Maintainers
Readme
SAMPLE CODE
const { RouteController, RouteBuilder, DB} = require("core-narmy")
const Mongo = require("mongoose");
const express = require('express');
const {models, dbConf} = require('./db') // schema and db connetion string + config
const collectionName = 'myCollection'
const logStorage = models.log
class ApiController extends RouteController{
/*--------------define your new controls--------------------------
connect(req,res){
......
}
-------------------------------------------------------------------*/
}
var controller = new ApiController(models[collectionName], logStorage, wymiswyu)
routes = {
prefixUrl: '/myapi',
routesConfig: [
{
method: 'post',
url: '/',
middlewares: [validationMiddleware, middleware1, middleware2 ...],
action: controller.create.bind(controller)
},
{
method: 'put',
url: '/',
middlewares: [validationMiddleware, middleware1, middleware2 ...],
action: controller.updateSoft.bind(controller)
},
{
method: 'delete',
url: '/:id',
action: controller.softDel.bind(controller)
},
{
method: 'post',
url: '/connect',
action: controller.connect.bind(controller)
}]
}
const apiRouter = new RouteBuilder(express.Router(), routes)
const app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(apiRouter.getPrefixUrl(), apiRouter.getRouter());
var port = process.env.PORT || 3000
const db = new DB(Mongo, confDb)
db.connect(Mongo, config)
app.listen(port, () => {
console.log("listen in port ", port)
})
POST => servername:port/myapi/ => create
PUT => servername:port/myapi/ => update soft
delete => servername:port/myapi/1 => delete soft document with id 1