node-setup-utility
v2.0.5
Published
Command Line Tool For Making Backend Service With Less Coding Using Node.js
Downloads
2
Maintainers
Readme
Node-Utility
About:
Installation:
npm i -g node-setup-utility
Usage:
1. Set up new NODE JS Project:
Step 1 : Intilize Node Project By Running Following Command and Give Necessary Details For package.json
npm init
Step 2 : Checkout Help Section Of The Command For More Understanding Of The Tool(Optional)
node-project -h
node-project --help
Step 3 : Run Following Command For Set Up Project, Enter Port Number to run your backend when asked
node-project basic
Step 4 : No more Stpes, That's it :star_struck:. 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 :airplane:.
node index.js
2. Establish MongoDB Connection:
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");
Note : You can access your input and also modify in .env file.
3. Establish MySQL Connection:
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");
Note : You can access your inputs and also modify in .env file.
4. Get Functions To Work With JWT:
:warning: 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.
Step 1 : Run Following Command, It will ask for JWT Secret
node-project jwt
Step 2 : Once last command completes it's execution, JWT functions will be available at middleware/auth.js.
Default Expiry is set to 24h, you can change it in the .env file.
How to Use this functions ?
- Generate JWT Token : Import generateJWT function from middleware.auth.js and Prepare Payload as pass it to function as below
example:
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.
example:
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.