ExpressJS Best Practices - ExpressJS

What are some of the best practices of ExpressJS?

Since ExpressJS is flexible with the way of doing things, file structure,there is huge scope for structuring the application the way you like. But maintenance becomes difficult once it grows in size, unless it has a well defined structure.
Here are some of the pointers which helps to create a well structured application.

Creating node and express applications.

  • Always begin a node project using npm init.
  • Always install dependencies with a --save or --save-dev to 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

This is commonly used project structure and is very intuitive.
Another popular approach to build websites with Express is using the MVC design pattern.

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:

You can also use a yeoman generator to get a similar structure.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

ExpressJS Topics