ExpressJS & Mongoose REST API structure: best practices?

12,385

Solution 1

Well, you can separate out your routes, db models and templates in different files. Have a directory structure something like this,

| your_app
| -- routes
| -- models
| -- templates
| -- static
    | -- css
    | -- js
    | -- images
| -- config.js
| -- app.js
| -- url.js
  • For each Mongoose model have a separate file placed in your ./models
  • In templates directory place your jade files. (Assuming you are using jade as your template engine). Though it seems like you are only serving JSON, not HTML. Consider using Jade if you want to render HTML. (Here are few other template engines you can consider going with)
  • ./static directory for static JS, CSS and XML files etc.
  • Things like db connections or 3rd party API keys and stuff can be put in config.js
  • In url.js have a procedure which take express app object as argument and extend upon app.get and app.post there in single place.

P.S. This is the approach I go with for a basic web app in express. I am in no way saying this the best way to follow, but it helps me maintain my code.

Solution 2

As codemonger5 said there is no right way of organising directory structure.

However, you can use this boilerplate application for creating REST APIs using Express and mongoose using ES6. We use the same directory structure in our production API services.

git clone https://github.com/KunalKapadia/express-mongoose-es6-rest-api
cd express-mongoose-es6-rest-api
npm install
npm start

Solution 3

There is no right way, but I did create a seed application for my personal directory structure to help my roommate with this.

You can clone it: git clone https://github.com/hboylan/express-mongoose-api-seed.git

Or with npm: npm install express-mongoose-api-seed

Share:
12,385

Related videos on Youtube

thomastuts
Author by

thomastuts

Updated on August 21, 2022

Comments

  • thomastuts
    thomastuts over 1 year

    I'm building a REST API with the use of NodeJS (Mongoose & ExpressJS). I think I have a pretty good basic structure at the moment, but I'm wondering what the best practices are for this kind of project.

    In this basic version, everything passes through the app.js file. Every HTTP method is then passed to the resource that has been requested. This allows me to dynamically add resources to the API and every request will be passed along accordingly. To illustrate:

    // app.js
    
    var express = require('express');
    var mongoose = require('mongoose');
    
    var app = express();
    app.use(express.bodyParser());
    
    mongoose.connect('mongodb://localhost/kittens');
    var db = mongoose.connection;
    
    var resources = [
      'kitten'
    ];
    
    var repositories = {};
    
    for (var i = 0; i < resources.length; i++) {
      var resource = resources[i];
      repositories[resource] = require('./api/' + resource);
    }
    
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function callback() {
      console.log('Successfully connected to MongoDB.');
    
      app.get('/:resource', function (req, res) {
        res.type('application/json');
        repositories[req.params.resource].findAll(res);
      });
    
      app.get('/:resource/:id', function (req, res) {
        res.type('application/json');
        repositories[req.params.resource].findOne(req, res);
      });
    
      app.listen(process.env.PORT || 4730);
    });
    

    -

    // api/kitten.js
    
    var mongoose = require('mongoose');
    
    var kittenSchema = mongoose.Schema({
      name: String
    });
    
    var Kitten = mongoose.model('Kitten', kittenSchema);
    
    exports.findAll = function (res) {
      Kitten.find(function (err, kittens) {
        if (err) {
        }
        res.json(kittens);
      });
    };
    
    exports.findOne = function (req, res) {
      Kitten.findOne({ _id: req.params.id}, function (err, kitten) {
        if (err) {
        }
        res.json(kitten);
      });
    };
    

    Obviously, only a couple of methods have been implemented so far. What do you guys think of this approach? Anything I could improve on?

    Also, a small side question: I have to require mongoose in every API resource file (like in api\kitten.js, is there a way to just globally require it in the app.js file or something?

    Any input is greatly appreciated!