Friday, February 17, 2017

ExpressJS - Best Practices

Unlike Django and rails which have a defined way of doing things, file structure, etc, Express is unopinionated on this. What this means is you can structure the application however you like. But as your application grows in size, its very difficult to maintain it if it doesn't have a well defined structure.
In this article we'll look at the generally used directory structures and seperation of concerns to build our applications. First we'll discuss common best practices for creating node and express applications.
  • Always begin a node project using npm init.
  • Always install dependencies with a --save or --save-dev. This will ensure that if you move to a different platform, you can just run npm install to install all dependencies.
  • Stick with lowercase file names and camelCase variables. If you look at any npm module, its named in lowercase separated with dashes. Whenever you require these modules, use camelCase.
  • Dont push node_modules to your repositories. Instead npm install everything on development machines.
  • Use a config file to store variables
  • Group and isolate routes to their own file. For example, take the CRUD operations in the movies example we sawin REST API page.

Directory Strucure

Websites

As I have already mentioned, Express does not have a community defined structure for creating applications. This is the project structure I've seen many people use and also have used it myself. This project structure for a website is very intuitive.
test-project/
    node_modules/
    config/
        db.js               //Database connection and configuration
        credentials.js      //Passwords/API keys for external services used by your app
        config.js           //Other environment variables
    models/                 //For mongoose schemas
        users.js
        things.js
    routes/                 //All routes for different entities in different files 
        users.js
        things.js
    views/
        index.pug
        404.pug
        ...
    public/                 //All static content being served
        images/
        css/
        javascript/
    app.js
    routes.js               //Require all routes in this and then require this file in app.js 
    package.json
There are also other approaches to build websites with express. Another one of them that is quite popular is using the MVC design pattern. You can read more about that here and here.

RESTful APIs

APIs are much simpler to design, they don't need a public or a views directory. Use the following structure to build APIs:
test-project/
    node_modules/
    config/
        db.js               //Database connection and configuration
        credentials.js      //Passwords/API keys for external services used by your app
    models/                 //For mongoose schemas
        users.js
        things.js
    routes/                 //All routes for different entities in different files 
        users.js
        things.js
    app.js
    routes.js               //Require all routes in this and then require this file in app.js 
    package.json
You can also use a yeoman generator to get a similar structure.

No comments:

Post a Comment