swati kiran
swati kiran

Reputation: 705

How to separate the routes and database functionality from appRouter.js using NodeJS and Express

in angularjs at express services I want all the calling services at one place.. something like this structure..

routes(some folder)
|-API (all calls)
|-Services
|-schemas(mongoose)
if these are my services

'use strict';

var express = require('express');
var router = express.Router();
var conveyModel = require('../model/Schema');

console.log("convey api router ready");
/* GET /convey listing. */
router.get('/', function(req, res, next) {
  console.log("convey api get '/'");
  conveyModel.find(function (err, convey) {
    if (err) return next(err);
    res.json(convey);
  });
});

/* POST /convey */
router.post('/', function(req, res, next) {
  console.log("convey api post '/'");
  console.log("retrieving:: " + req.body);
  conveyModel.create(req.body, function (err, post) {
    console.log("saving:: " + post);
    if (err) return next(err);
    res.json(post);
  });
});
module.exports = router


i want to call all services in other Api js somthing like this,

router.post('/api/v1/login', auth.login);
router.get('/api/v1/me', users.getAll);


I'm not able to understand how it works... if there is some working example, with what I want it would be great.

Upvotes: 1

Views: 413

Answers (2)

swati kiran
swati kiran

Reputation: 705

silly me found answer long back but wanan update what i found!!!
in API.js all api calls i gathered like this

var express                     = require('express');
var router                      = express.Router();
var fact                    = require('../services/factRouter');

 router.get('/fact', fact.getAll);
 router.post('/fact/', fact.create);
 module.exports = router;


and in services

'use strict';

var express = require('express');
var router = express.Router();
var factModel = require('../model/factSchema');

var users = {

  getAll: function(req, res, next) {
    console.log("fact api get '/'");
    factModel.find(function (err, fact) {
      if (err) return next(err);
      res.json(fact);
    });
  },

  create: function(req, res, next) {
    console.log("fact api post '/'");
    console.log("retrieving:: " + req.body);
    factModel.create(req.body, function (err, post) {
      console.log("saving:: " + post);
      if (err) return next(err);
      res.json(post);
    });
  }
};
module.exports = users;

and obviously in schema

'use strict';

var mongoose = require('mongoose');

var factsSchema = new mongoose.Schema({
    title: { type: String },
    description: { type: String },
});

module.exports = mongoose.model('facts', factsSchema);

console.log("facts schema defined") 


yaaa dat was easy !!!

Upvotes: 1

Yerken
Yerken

Reputation: 1942

You can do something like this, in your routes.js can do this:

var express = require('express');
//by right app should have been created beforehand in index.js or app.js
//and just imported here
var app = express(); 

app.use('/api/users', require('./api/users');
...

then in /api/users create an index.js file with the following content:

var express = require('express');
var router = new express.Router();
var controller = require('./controller');
var auth = require('./service');

router.get('/', auth.isAuthenticated(), controller.getProfile);

module.exports = router;

Put your router controller in ./api/user/controller.js and logic (e.g. database layer or middleware) into ./api/user/service.js.

This is not the only architecture pattern you can use, but it must give you some ideas where to go from here

Upvotes: 0

Related Questions