-
- Folder Renaming
- Configuration
-
With docker Without docker Docker Install Dependencies Database Setup Start the Server
Follow these steps to get started with Falcon Boilerplate:
-
Folder Renaming:
Start by renaming thedemo_ssl
folder tossl
anddemo_settings
tosettings
. -
Configuration:
Configure your application settings in thesettings/dev.js
file for development andsettings/prod.js
for production. You will get the settings inside every request and socket events. The settings used will be determined by the NODE_ENV variable inside the index.js file at the root. -
Install Dependencies
Run the following command to install project dependencies:npm install # or yarn install
-
Database Setup
For database you can isntall mysql in your system and place necessary information at settings/dev.js and settings/prod.js. -
Start the Server
Start the server using nodemon to enable hot-reloading during development:yarn dev
Follow these steps to create a new service:
-
Create the service root file
Start by organizing your services within theservices
directory. Each service should have its own dedicated folder and a corresponding file for the service entry points also a single or multiple entity file for building the core logic.
Example:- services | - user | - user.js | - user.entity.js
-
Generate service and entity with vs code snippets
If you are using vs code. Then there is two vs code snippets to generate the service and entity code with basic crud operation. If you follow this step then you don't need to follow step number 3 and 4.Trigger Content service
service code with basic crud api routes and socket function.
entity
entity for the basic crud apies.
-
Create the api routes and register socket listeners in the service root file
Service Root File (e.g., user.js):const { create, handleClickButton } = require('./user.entity'); /** * INSTRUCTIONS: * 1. Call API and socket handler functions from entity file (e.g., user.entity.js). */ /** * Define API routes for user management. */ function userApi() { /** * POST /user * @description This route is used to create a user. * @response {Object} 201 - The new user. * @body {Object} - The data to create a user. */ this.router.post('/user', create(this)); } /** * Register event handlers for user-related events. */ function userSocket() { this.socket.on('clickedButton', handleClickButton(this)); } module.exports = { userApi, userSocket };
-
Create entity functions
Entity File (e.g., user.entity.js):const TABLE_NAME = 'user'; module.exports.create = ({ db }) => async (req, res, next) => { try { const { role } = req.params; let { first_name, last_name, phone, email, password } = req.body; password = await bcrypt.hash(password, 8); await db.execute(`CALL register(?, ?, ?, ?, ?, ?)`, [first_name, last_name, phone, email, password, role]); res.status(201).send({ message: 'User registration successful' }); } catch (e) { next(e) } }; module.exports.handleClickButton = async ({ db }) => { // Implement the functionality you need. }
Note: In this boilerplate, all functions are connected to the Falcon class. This gives you access to various features, like I have accessed the db above. It also allows you to easily add new tools and functionalities to the Falcon class and access them globally.
Lastly For making the service available for your clients you have to inject that in the app.
Follow these steps:
-
Inject api service:
const { errorMiddleware } = require("./middlewares"); +const { userApi } = require("./user/user"); function apiServices() { + userApi.call(this); this.router.use(errorMiddleware(this)) };
-
Inject socket service:
const { errorMiddleware } = require("./middlewares"); +const { userApi, userSocket } = require("./user/user"); function apiServices() { userApi.call(this); this.router.use(errorMiddleware(this)) }; function socketServices() { + userSocket.call(this); };
As simple as that. Your services are good to go now.
In the entity, if you invoke the next
function of express with an Error
instance as its first parameter, the errorMiddleware
function will be triggered. The error will be permanently logged in the data/server_error/{year}/{month}/{day}.log
file, and the client will receive a 500 response with a reference id. If reference id is not found in the response, it means the middleware failed to save the error. The error log can be found in the console.
module.exports.create = ({ db }) => async (req, res, next) => {
try {
const user = await db.create({ table: TABLE_NAME, payload: { data: req.body } });
return res.status(201).send(user);
} catch (e) { next(e) }
};
You can place your client code inside the client
folder. The Falcon boilerplate will search for the index.html
file to serve the client.
# Creating bridge type network (if not already exists)
# The database and the api will be in this network.
docker network create -d bridge falcon
Two Docker Compose files are provided:
compose_dev.yml
: For development mode.compose.yml
: For production mode.
# Running in Development Mode
docker compose -f compose_dev.yml up --watch
# Rebuilding API Image (if needed)
docker compose -f compose_dev.yml up --build --watch
# Running in Production Mode
docker compose up -d