Skip to content

It is a boiler plate project for Node, Express and MongoDB.

Notifications You must be signed in to change notification settings

elbassel/node-project-kickstarter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

# node-project-kickstarter Node project kick starter is a boilerplate project to get started developing your application quickly.
Main Technologies: Nodejs, Express, MongoDB, Mongoose, JWT, Passport, Node Mailer, DOTENV.
Don't bother your self by implementing Mongoose, Authentication, logs, Mailing,...
It's already implemented, just tune what you need in the project and get started quickly.


All sensitive information is excluded to be written in any file in the repository such as passwords, JWT key, etc.
So, Dotenv package is used to handle it, create a .env file under config folder and define it in the app file.
require('dotenv').config({path: './config/.env'})
In .env file:
KICK_DB_NAME=tempDB
KICK_DB_PORT=27017
KICK_DB_HOST=localhost
LOG_KICK_STARTER_LEVEL=info
MAIL_SUPPORT_PASSWORD=XXXXXX
JWT_KEY=@ass!))98&&&?>*F*
SESSION_TIMEOUT=60

Logs

One of the most important components is to log some info to help you debug your application.
Pino is one of the fastest logging tools.
Three Serializers are implemented, you can find them in ./congi/components/logger/log-serializers folder:

  • requestMax: to log the entire HTTP request
  • requestMin: to log some info from the request
  • fnction: to log the function name

To use the logger component in your module: const logger = require('../config/components').logger;
You have to provide the "LOG_KICK_STARTER_LEVEL" in .env, in our project here its info Samples of using the logger component:
logger.debug({path: __dirname, fnction: createUser, user : userObj}, "user created successfully");

Database (MongoDB and Mongoose)

Mongoose is used as ODM.

Provide DB environment variables in .env file

KICK_DB_NAME=tempDB
KICK_DB_PORT=27017
KICK_DB_HOST=localhost

You can have a look on users schema in users/users_schema.js

MVC Pattern

MVC pattern is applied, check users folder.

Register or Signup

users_route have an endpoint to create a user: "post /users"

Hash and salt are used to store passwords, passwords are not saved, only salt and hash. 

salt and hash are creating for the document in "users_schema.js" by using mongoose hooks.

Moreover, salt and hash are stored in a separate collection called auth, a hashed index is creating for userId in auth collection.

 

Authentication

JASON WEB TOKEN and passport with local strategy

Two environment variables are used to set session timeout in minutes and the key that is used to generate JWT token

JWT_KEY=@ass!))98&&&?>*F*
SESSION_TIMEOUT=60

To start authenticating some end points, you can get reference of auth object as following:
const auth = require('../config').components.auth;
And use it with any end points:
router.get('/:id', auth, (req, res)=>{
To login:
submit username and password to: post /login
The response will be json object contains jwt token and logged in user object.

Email Notifications

Nodemailer is used to send emails.
You can define many emails as much you need, please config/components/mail/mail.js.
You can add new one such as the support one.

You have to add to the password as environment variables:
MAIL_SUPPORT_PASSWORD=XXXXXX

Your templates will be under utils/mail-templates

To start using this copmonent:
const MailUtil = require('./utils/MailUtils');
const configMail = require('./config').components.mail;
const CONSTANTS = require('./utils/constants');
MailUtil.sendEmail(configMail.SUPPORT.CONFIG, CONSTANTS.MAIL_TEMPLATES.SUPPORT, "Testing Mails", {"some" : "data"}, "elbassel.n13@gmail.com");

About

It is a boiler plate project for Node, Express and MongoDB.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages