Setting up a new application in node requires a good amount of work. Most tutorials online do not follow patterns that will scale to larger teams in real production scenarios. For anyone starting new or even for experienced developers that are new to node.js, its very common to get confused and they end up spending a lot of time in just setting up the base application.
The aim of this project is to provide a starter app with patterns, practices and structure that can bse used to create a production level service.
This project is an opinionated take on creating a base application that can be reused for creating any type of node.js api's.
You can swap out libraries and replace them with other libraries you are comfortable with.
If you already have an application and are trying to solve some issue, this would be a good starting point to see how those issues are solved in other applications.
This project contains:
- Node.js api server using express
- React front end
Firebase Authentication:
Authenticate users via firebase on the client and while making api calls, pass the token asAuthorization Bearer <token>
header to access secure content.MongoDB:
- Save data in MongoDB usingmongoose
.Auth middleware:
Secure any api endpoint by simply adding the firebase auth middleware.Logger:
Use winston logger to log to a file.config
&dotenv
modules for managing configurations in your application. config module along withdotenv
provides a convenient way to manage configurations in local development as well as production deployments.firebase-admin
sdk to manage firebase.express
server for creating REST api's.helmet
for basic server side security.cors
for allowing requests coming from web client.Request Context
usingAsyncLocalStorage
for easily maintaining and logging correlation throughout the the request execution lifecycle.- Run the server in dev mode with support for typescript compilation and server restart automatically whenever you change code.
The server code written in typescript and is divided into modules and layers.
config
|-default.json // Contains configurations and secrets that are used by the application
controllers
|-profilesController.ts // Controllers that contains the actual implementation for an api endpoint
logger
|-//Winston logger related
logs
|- // Logs directory created by winston
middleware
|- firebaseAuthMiddleware.ts // Directory to hold all your middlewares
models
|- // Models and business entities
services
|- // Services are wrappers that encapsulate interaction with services/db's outide this api. Ideally you should have one service per external endpoint
types
|- // Typescript types and interfaces
utils
|- // Utility and helper functions
- index.ts // Starting point of the application that initializes and starts the server
- server.ts // Actual express server
Follow the steps below to run the node api server:
Steps
- Rename
.env.sample
to.env
. This file contains all your secrets and connection strings. - Provide the values required in the
.env
file. - To obtain firebase admin related config values, check the docs here: Firebase Admin Setup
On Windows:
Open a command prompt in the root of the repository and run the below command and navigate to the node-server
directory:
cd node-server
Install npm packages
npm install
Run the server in dev mode
npm run dev
Create a build
npm run build
This will create the build in /dist
folder. To deploy this to production, you will need to copy the .env
file to the /dist
folder, provide the setting in the .env
file based on the environment where you want to deploy your application. These values could be different for various environments.
A basic react fontend bootstrapped using create-react-app and is integrated with the node-server. The frontend itself does not use any UI library as people have different choices and prefernces. The project provides a frontend that calls the backend node api to get data from MongoDB. You can use these together as a starting point and extend this to a larger application.
Follow the steps below to run the nreact frontend:
Steps
- Rename
.env.sample
to.env
. This file contains all your secrets. - Provide the values required in the
.env
file. - Frontend uses firebase for authentication so you will have to setup a firebase project and get the requried values from firebase console.
- opne a cmd prompt inside the
react-client
folder and run the commands below:
On Windows
Install npm dependencies
yarn
or npm install
yarn start
or npm run
- Built using Typescript.
- React app using CRA.
- No UI library dependency so you can use any library you want.
- Redux/Redux-Toolkit for state management fetching data.
- Axios for api calls.
- Firebase for user authentication.
- React-Router for navigation
- Private routes using react-router to handle auth/unauth routes.
- Nested routing for Dashboard like applications.
- Simple way to manage api response status to show loading status.
- PWA support by opting into PWA features provided by create-react-app
The server code written in typescript and is divided into components and layers.
components
|- // Core UI components that you might share across views
hooks
|- useAuth.ts // Hook that handles firebase authentication
|- // Reusable react hooks
services
|- // Any interaction with external services should be in a service. Ideally one file for each external service.
store
|- // Redux store and states implemented using redux-toolkit
types
|- // Typescript types
utils
|- // Helpers and utils
views
|- // Folder that contains pages and their sub views
App.tsx // Actual react application. Load redux provider.
index.tsx // Starting point of application
Routes.tsx // Application routes
service-worker.ts // PWA related file from CRA
serviceWorkerRegistration.ts // PWA related file from CRA
setupTests.ts // File from CRA
- Support for GraphQL
- Integrate Socket.io
- Encapsulate DB layer
- Swagger integration
- Api input validation
- Error handling middleware