Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpressJS & Mongoose REST API structure: best practices?

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!

like image 284
thomastuts Avatar asked Sep 03 '13 13:09

thomastuts


People also ask

What is ExpressJS used for?

What Is Express JS? Express is a node js web application framework that provides broad features for building web and mobile applications. It is used to build a single page, multipage, and hybrid web application. It's a layer built on the top of the Node js that helps manage servers and routes.

What is the difference between ExpressJS and NodeJS?

js web application framework that provides a robust set of features for web and mobile applications. In other words, NodeJS is the package, which provides the JavaScript run-time environment, whereas Express is a framework that sits on top of NodeJS and helps us to handle requests and responses.

What is ExpressJS in simple terms?

Express. js is a free and open-source web application framework for Node. js. It is used for designing and building web applications quickly and easily. Web applications are web apps that you can run on a web browser.


3 Answers

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.

like image 171
Sushant Gupta Avatar answered Sep 23 '22 19:09

Sushant Gupta


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

like image 36
hboylan Avatar answered Sep 25 '22 19:09

hboylan


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
like image 30
Kunal Kapadia Avatar answered Sep 23 '22 19:09

Kunal Kapadia