jsm-connector-interfaces
v1.0.5
Published
This project structure is inspired by Ruby on Rails and is designed to organize your NodeJS application efficiently.
Downloads
3
Readme
NodeJS Project Folder Structure
This project structure is inspired by Ruby on Rails and is designed to organize your NodeJS application efficiently.
Folder Structure
/app
├── config/
│ ├── sample.config.js
│ ├── index.js
├── database/
│ ├── sample.database.js
│ ├── index.js
├── routes/
│ ├── sample.routes.js
│ ├── index.ts
├── utils/
│ ├── sample.util.js
│ ├── index.js
├── middleware/
│ ├── sample.middleware.js
│ ├── index.js
├── models/
│ ├── sample.model.js
│ ├── index.js
├── controllers/
│ ├── sample.controller.js
│ ├── index.js
├── helpers/
│ ├── sample.helper.js
├── /server.js
/samples
├── .env.sample
/node_modules
/package.json
/.env
Project Structure Brief
Config & Sample
Configuration is divided into:
- System Configuration
- App Configuration
- App Keys
Store configuration variables in .env
files and commit only the structure with dummy values in the /samples
folder.
Store the configuration variables in .env
(dotenv) which are necessary for configuring your application like App Port, Environment (Production, Staging, etc.). .env configuration will be available across your app as they are set globally as ENV variables.
You could then create multiple app-relevant configuration files like:
- Database Connection: Database-specific configurations like Host, Port & Name.
- App Configuration: Acceptable Request Payload Size, Blacklisting IPs or Regions, etc.
- Auth Configuration: OAuth Callback URLs, etc.
And last but not the least, you could create multiple files and store relevant keys, like OAuth Client & Secret Keys, Database Keys, Mailer Keys etc.
Important: Do not commit any of these configuration files to Git. Copy the configuration structure with dummy values and commit to git using sample files under the /sample folder.
Database
Manage your database connections (e.g., Redis, MongoDB) here. Include configuration and keys for initializing connections, handling events, etc.
Routes
Organize your routes into multiple files for scalability:
- User Routes
- Post Routes
Example:
const express = require("express");
const userRoutes = require("@routes/user.routes");
const postRoutes = require("@routes/post.routes");
app.use("/users", userRoutes);
app.use("/posts", postRoutes);
Utils
Utility functions, such as logging, which are static and unrelated to other classes/files.
Middleware
Middleware functions for tasks like body parsing, error handling, authentication, enabling CORS, and setting view engines.
Models
Each collection/table has a standalone model file. For example, user.model.js
for user-related data operations.
Controllers
Controllers handle incoming requests, render pages, send JSON payloads, and perform API actions (POST, PUT, DELETE).
Helpers
Helpers are dynamic and related to specific controllers. They can parse user input, modify data before database storage, etc.