Node_Utility is a CMD tool for faster development of backend project which are based on NODE-JS. As NODE-JS Developer we know that there is a lot of repetative code we are writing across multiple projects from setting up projects to writing helper functions and connecting with databases. Use this tool and save your time of writing same code again and again. Use our command for set up new node js project, helper functions, database connection and JWT session managment. Now only focus on business logic of your app and other things, leave it to us 🤝.
npm i -g node-setup-utility
Step 1 : Intilize Node Project By Running Following Command and Give Necessary Details For package.json
npm init
node-project -h
OR
node-project --help
node-project basic
Step 4 : No more Stpes, That's it 🤩. Once last command completes it's execution, all the files will be generated.
You can run your project by typing following command. It will give you the url in the console on which your backend is working ✈️ .
node index.js
Step 1 : Run Following Command, It will ask for mongoDB url and DB name, you will need to enter it when asked
node-project mongocon
Step 2 : Once last command completes it's execution, connection file can be accessed at database/mongodbConnection.js.
Now You can use it anywehre in the project, recommanded way is to include in in the app.js file using following way.
require("path of your database/mongodbConnection.js");
Step 1 : Run Following Command, It will ask for MySQL connection configurations, you will need to enter it when asked
node-project mysqlcon
Step 2 : Once last command completes it's execution, connection file can be accessed at database/mysqlConnection.js.
Now You can use it anywehre in the project, recommanded way is to include in in the app.js file using following way.
require("path of your database/mysqlConnection.js");
⚠️ To Use this, You will need our helper function which is only available if you setup project using our tool. In Future, we will add functionality to setup only helper functions, but currently you can't use this feature if you haven't setup your project using our tool.
node-project jwt
Step 2 : Once last command completes it's execution, JWT functions will be available at middleware/auth.js.
- Generate JWT Token : Import generateJWT function from middleware.auth.js and Prepare Payload as pass it to function as below
const { generateJWT } = require("./middleware/auth.js");
// create payload
const payload = {
name : "demo",
email : "email",
isSuperAdmin : false,
};
// generate token
const token = generateJWT(payload);
- payload can contain any information of your choice, which you can access when decrypting token.
- you can send this token to client and when client access any route which require authorized user, you can use our authMiddleware, which will verify user is authorized or not, if authorized you can access it's payload which you set during generating that token in your route. If not authorized it will generate error.
router.patch("/profile", validator(updateUserSchema), authMiddleware, Update);
- verify token by your self :
const to = require("await-to-js").default;
const { verifyToken } = require("./middlewares/auth");
// access token
const { token } = req.body;
// verify token
const [error, payload] = await to(verifyToken(token));
- if token valid in above code, error object will be null and you can use payload. Otherwise, error will have some message.