Posts

Showing posts from December, 2019

How to Hash Passwords in Node.js Using Bcryptjs, Express and Mongoose

Introduction Storing plaintext passwords in a database is one of the most critical security vulnerabilities a web application can have. If a database is compromised, every user account is instantly exposed. To protect user credentials, modern backend applications rely on secure cryptographic hashing functions like bcrypt . In this tutorial, we will walk through how to build a secure user registration and login API in Node.js using Express, Mongoose, and bcryptjs . What is Bcrypt? Bcrypt is a password-hashing function designed by Niels Provos and David Mazières, based on the Blowfish cipher, and introduced at USENIX in 1999. Built-in Salt Protection: Bcrypt automatically incorporates a salt (random data) into the hashing process to protect against rainbow table attacks. Adaptive Work Factor: It is an adaptive function, meaning the iteration count (cost factor) can be increased over time to keep pace with growing computer processing power, making it highly resistant to ...

How to Use Sequelize-CLI to Create Tables and Relationships in Node.js

Introduction In this article, we will explore how to use Sequelize-CLI for creating database tables and setting up powerful relationships between multiple tables using foreign keys in Node.js and MySQL. Project Structure |--------- config |--------- config.json |--------- models |--------- index.js |--------- department.js |--------- emp.js |--------- migrations |--------- [timestamp]-create-department.js |--------- [timestamp]-create-emp.js |--------- seeders |--------- app.js |--------- package.json Setup The Node Project Let's begin by initializing our workspace: Open your console and create a new directory: mkdir seqlcli Navigate into that directory: cd seqlcli Initialize the Node.js project by running: npm init This will prompt you for basic project metadata like name, version, and author, generating a package.json file. Install P...

How to Use Sequelize ORM in Node.js with Express & MySQL

Prerequisites Node.js MySQL What Is Sequelize? Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication, and more. Sequelize follows SEMVER and supports Node v6 and above to utilize ES6 features. Express and Sequelize Project Structure |--------- config |--------- db.js |--------- controllers |--------- memberController.js |--------- models |--------- member.js |--------- routes |-------- members.js |--------- views |------- partials |------- header.ejs |------- footer.ejs |------ home.ejs |------ edit.ejs |--------- app.js |--------- package.json Project Setup Let's begin by setting up our workspace: Open the console and type mkdir followed by your directory name. ...