All Projects → anandundavia → express-api-structure

anandundavia / express-api-structure

Licence: other
Structure for Express based API server

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects
Dockerfile
14818 projects

Projects that are alternatives of or similar to express-api-structure

faeshare
MERN based social media web app made with the help of Next.js, Socket.io and TailwindCSS.
Stars: ✭ 37 (-53.16%)
Mutual labels:  expressjs
nestjs-cookie-session
Idiomatic Cookie Session Module for NestJS. Built on top of `cookie-session` 😻
Stars: ✭ 35 (-55.7%)
Mutual labels:  expressjs
vector-tiles-generator
Easily generating mapbox vector tiles from postgis database (without mapnik dependency)
Stars: ✭ 20 (-74.68%)
Mutual labels:  expressjs
mean-stack
MEAN stack Mongoose, Express, Angular6, Node
Stars: ✭ 22 (-72.15%)
Mutual labels:  expressjs
node-express-mongo-passport-jwt-typescript
A Node.js back end web application with REST API, user JWT authentication and MongoDB data storage using TypeScript
Stars: ✭ 51 (-35.44%)
Mutual labels:  expressjs
QuickStarter
Quick nodejs (Express) + docker + mongodb + JEST(testing) setup.
Stars: ✭ 12 (-84.81%)
Mutual labels:  expressjs
task-manager
Task Manager App
Stars: ✭ 19 (-75.95%)
Mutual labels:  expressjs
mock-req-res
Extensible mock req / res objects for use in unit tests of Express controller and middleware functions.
Stars: ✭ 39 (-50.63%)
Mutual labels:  expressjs
api-server
📡 API server for api.cdnjs.com - The #1 free and open source CDN built to make life easier for developers.
Stars: ✭ 48 (-39.24%)
Mutual labels:  expressjs
uploading-files-react-node
Uploading files with React.js and Node.js
Stars: ✭ 33 (-58.23%)
Mutual labels:  expressjs
inventory-demo
a simple MERN stack CRUD app example
Stars: ✭ 15 (-81.01%)
Mutual labels:  expressjs
react-full-stack-starter
🎈Full-stack React boilerplate using `create-react-app`, Babel, Node.js, and express
Stars: ✭ 22 (-72.15%)
Mutual labels:  expressjs
watson-discovery-analyze-data-breaches
A Node.js application that demonstrates how to Import, Enrich, and see Insights about data using Watson Discovery.
Stars: ✭ 20 (-74.68%)
Mutual labels:  expressjs
Food-Ordering-Application
No description or website provided.
Stars: ✭ 20 (-74.68%)
Mutual labels:  expressjs
roboserver
Control OpenComputers robots without writing any code!
Stars: ✭ 52 (-34.18%)
Mutual labels:  expressjs
lightning-rpc-explorer
Simple, self-hosted lightning network explorer.
Stars: ✭ 45 (-43.04%)
Mutual labels:  expressjs
trivin
⚡️Setup your entire project quickly and easily with 1-line command ⚡️
Stars: ✭ 58 (-26.58%)
Mutual labels:  expressjs
Book-Trading-Club
A sample application using node.js api and vuejs
Stars: ✭ 20 (-74.68%)
Mutual labels:  expressjs
spotify-auth-code-example-vue
VueJS boilerplate app with Spotify API authentication and data fetching.
Stars: ✭ 12 (-84.81%)
Mutual labels:  expressjs
buddies-tube
A video streaming platform
Stars: ✭ 14 (-82.28%)
Mutual labels:  expressjs

Structure

src contains the whole source code of the api. Inside src, there are three directories: api, config, constants and one index.js file which will be the entry point to the application

constants directory

index.js will export all the constants required in the api.

  • constants which are common across environments like dev and prod should be defined in constants.common.js

  • constants which are specific for environments should be defined in constants.dev.js or constants.dev.js. Make sure to have the same shape of constants in all the constants.[env].js files

  • DO NOT include sensitive information like credentials of database in the constants, they should be defined in the .env file which is explained

config directory

This directory will have self-contained config files for different modules in your api. In the boilerplate, express.js and session.js are included which configures the appropriate modules.

While creating new config files, in the mind that self-contained config files will help in scaling the application as well as make it more maintainable, For example, If in future you want to shift to sails from express, all you need to do is create one sails.js file, require it in the src/index.js and done!

You can even take this to one level further by doing express.dev.js and express.prod.js and require appropriate file in src/index.js based on the env. For the most applications however, that part will be taken care by constants. But if needed, above can be done as well

api directory

This directory will be the one you will be working with most of the time This directory contains more directories:

  • controllers
  • middlewares
  • repository
  • routes
  • services
  • utils
  • validations

controllers

A controller will have all the business logic related to one object or one api path ( /v1/product )

Naming convention: If your api is /v1/product then name your controller as product.controller.js product.controller.js will have all the business logic that will be performed at /v1/product/... endpoints. API and controller function mapping:

API                         | controller function
                            |
GET: /v1/product/all        | `exports.getAllProducts`
GET: /v1/product/:productID | `exports.getSingleProduct`
POST: /v1/product           | `exports.createProduct`
PUT: /v1/product/productID  | `exports.updateProduct`

A product.controller.js file should ideally use product.service.js and response.service.js files located in api/services directory

middlewares

Any common functions that are needed across routes should be inside this directory. Middlewares with functionalities of error handling and authentication are the most common across routes and thus are included in this boilerplate

Convention: In case of express, all the middlewares should be a function which takes either (req, res, next) or (error, req, res, next) as argument

repository

All the queries to database should reside here. In this boilerplate, mongo directory is included as example. Which will have all the queries to the mongo database.

Convention:

  • A separate directory for each repository/database used. For example if you use redis and mysql, you will have two directories inside repository
  • Each database specific directory should have [name-of-db].repository.js file (mongo.repository.js is included in the example). This file should have logic to open and close the connection to the database. This files also multiplexes the queries fired on that database. You might want to have one more directory named collection or table inside each database specific directory. If you have two collection/table named product and user in your database, then collection directory should have product.collection.js and user.collection.js ( or table directory should have product.table.js and user.table.js ).
  • Each [entity].collection.js or [entity].table.js should at least export two members. A function named setDatabase and An object named queries containing all the query functions. Explore the boilerplate example for more information
  • All the queries should be exported in such a manner that a controller or a service using that query should not know on which database it is being fired. For example a controller should import a query like: import { findUser, setSession } = require('../repository'); Chances are, findUser is exported by mongo and setSession is exported by redis. Use object destructuring at appropriate places to achieve the same

routes

All the API routes.

Convention:

  • Create version wise directories ( v1, v2 )
  • Each version directory should have index.js.
  • If there are apis of user and product, the v1 directory should have user.route.js and product.route.js

Look at the example given in the boilerplate and read the comments to get the better idea

services

A service should be created:

  1. To increase code reuse ( response.service.js )
  2. To encapsulate complex business logic which is outside of the scope of a controller
  3. If you need to manage some sort of state in the API ( when was the last time some cron job ran )

If a service maintains some state, it must be a singleton

utils

Utility classes and functions that are used frequently in the API ( which can not be inside middlewares or can not be a service on their own )

validations

Validations for each api.

Convention:

  • All the validations of /v1/product/... route should be inside product.validation.js Have a look at the user.validation.js included in the boilerplate example.

Some Details combining everything together

To create a /v1/product/... API routes:

  1. Create product.controller.js in controllers
  2. Create product.route.js in routes
  3. Create product.validation.js in validations
  4. Create product.collection.js in repository/mongo
  5. You might want to create product.service.js inside services

Inside the routes/v1/index.js file, the routes should be mounted like

const productRoutes = require('./product.route');
...
router.use('/product', productRoutes);

`product.route.js` should look like:
const validate = require('express-validation');

const controller = require('../../controllers/product.controller');
const validation = require('../../validations/product.validation');


...
router.route('/')
    .get(validate(validation.getProduct), controller.getProduct)
    .post(validate(validation.createProduct), controller.createProduct)

`product.controller.js` should make use of functions exported by `product.collection.js`

Sensitive information and .env files

Sensitive information like database credentials and keys to sign cookies should not be included in constants file. They should be in .env file

.env must never be committed to version control. .env.example file should be committed to let other developers know what env variables should be defined

Checkout .env.example and .env to get the better idea

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].