Skip to main content

Project Template

Framework X is a meticulously designed solution to kickstart your project. With predefined best practices and structures. Embrace our expert recommendations or customize. Either way, you're set up for success, saving time and ensuring quality from the get-go

/
├── app
│   ├── controllers
│   │   └── index.controller.js
│   ├── helpers
│   │   └── methods.js
│   ├── middlewares
│   │   ├── error.middleware.js
│   │   ├── logger.middleware.js
│   │   └── validators
│   │   └── index.validations.js
│   ├── routes
│   │   └── api.route.js
│   ├── server.js
│   └── tests
│   ├── index.fail.validation.test.js
│   ├── index.get.test.js
│   └── index.post.test.js
├── http
│   └── index.http
├── log
│   ├── access
│   └── error
├── package.json
└── x
File/Directory NameDescription
app.controllersThe controller directory. it contains all the controller files. Nested directories are also supported.
app.helpersAny sort of helpers required by the project are contained in this directory. By default, it comes with methods.js with some handy methods.
httpThis directory contains the .http files. You can find more details about .http files here
logStores access and error logs by date.
app.middlewaresThis directory contains all the middleware. Framework X comes with some default middleware which we will discuss in detail in further documentation.
app.middlewares.validatorsThis directory contains request validators. Framework X uses express-validator under the hood.
app.routesThis directory contains all the API routes. You can find more details about Express Routes here.
app.testsThis is the "tests" directory
app.server.jsCreated Server from the app instance provided by index.js. "server.js" is the entry point for Framework X
xCLI access point.

helpers/methods.js

This file contains some handy methods that can be used throughout the project. It is a good practice to keep all the helper methods in this file. You can add more methods as per your project requirements.

successResponse

This method is used to send a success response to the client.

/**
*
* @param message
* @param payload
* @param statusCode
* @returns {
* {
* message: *,
* status: boolean,
* status_code: number,
* package: *
* }
* }
* @example
* const { successResponse } = require("../helpers/methods")
* res.status(200).json(successResponse("User created successfully", user, 200))
*/
exports.successResponse = (message, payload, statusCode = 200) => {
return {
status: true,
status_code: statusCode,
message: message,
package: payload
}
}

failResponse

This method is used to send an error response to the client.

/**
*
* @param message
* @param payload
* @param statusCode
* @returns {
* {
* message: *,
* status: boolean,
* status_code: number,
* package: *
* }
* }
* @example
* const { failResponse } = require("../helpers/methods")
* res.status(400).json(failResponse("Validation failed", errors.array(), 400))
*
* */
exports.failResponse = (message, payload = null, statusCode = 400) => {
let response = {
status: false,
status_code: statusCode,
message: message
}

if (payload) {
response.payload = payload
}

return response
}

middlewares

This directory contains all the middleware. Framework X comes with 2 default middleware menthioned below.

  1. logger.middleware.js
  2. error.middleware.js

logger.middleware.js

This middleware is used to log all the requests and responses. It uses simple-node-logger under the hood.

const accessLog = require("simple-node-logger").createSimpleLogger({
logFilePath: "./log/access/" + new Date().toLocaleDateString().split("/").join("-") + ".log",
timestampFormat: "YYYY-MM-DD HH:mm:ss"
})

/**
*
* @param req
* @param res
* @param next
* @returns {*}
*/
module.exports = (req, res, next) => {
let reqObject = {
method: req.method,
path: req.path,
param: req.params,
body: req.body
}

let resObject = {
statusCode: res.statusCode
}

accessLog.info(
JSON.stringify({
reqObject,
resObject
})
)

next()
}

error.middleware.js

This middleware is used to handle all the errors. It uses simple-node-logger under the hood. Also, Framework X use express-async-errors to handle async errors. Any exception thrown will be handled by this middleware and a fail response will be sent to the client.

const methods = require("../helpers/methods")
const errorLog = require("simple-node-logger").createSimpleLogger({
logFilePath: "./log/error/" + new Date().toLocaleDateString().split("/").join("-") + ".log",
timestampFormat: "YYYY-MM-DD HH:mm:ss"
})
/**
*
* @param error
* @param req
* @param res
* @param next
* @returns {*}
*/
// eslint-disable-next-line no-unused-vars
module.exports = (error, req, res, next) => {
errorLog.error(error.message)
return res.status(process.env.EXCEPTION_CODE).send(methods.failResponse(error.message))
}

middlewares/validators

This directory contains request validators. Framework X uses express-validator under the hood.

server.js

This file creates the server from the app instance provided by index.js. "server.js" is the entry point for Framework X.

const app = require("@3rdplanet/x-core/app")
const port = process.env.PORT
const errorMiddleware = require("./middlewares/error.middleware")
const accessLogMiddleware = require("./middlewares/logger.middleware")

console.log(`Node environment: ${process.env.NODE_ENV}`)
app.listen(port, () => {
console.log(`Example app listening at port http://localhost:${port}`)
})

app.use(errorMiddleware)
app.use(accessLogMiddleware)

x

This is the CLI access point. It is used to control the CLI utilities of Framework X. You can find more details about CLI here

Support Framework X

  • You can also support the project by following the organization and Star ⭐ the project on GitHub
  • Contribute by submitting issues and pull requests
  • Share the project with your friends and colleagues, any and all support is appreciated. 🙏
  • If you find this Framework useful, We will always appreciate a strong cup of coffee.