Posts

Showing posts with the label node.js

How to Upload Files Using AJAX in Node.js(Express)

Introduction In this blog, we will see how to upload an image file using AJAX. We will use the FileReader API to read the selected file, send the file data to the server via AJAX, and handle and save it as an image on the server side. Project Structure |--------- views |--------- demo.ejs |--------- app.js |--------- package.json Setup Folder Open your command prompt and create a new folder using the following command: mkdir upload Navigate into the newly created folder: cd upload Setup Node In Folder Initialize the Node.js environment by running: npm init -y This command creates a package.json file indicating that Node is initialized in the folder. Install Packages Install the required packages for building the application (Express, Body-Parser, EJS, and jQuery): npm install body-parser ejs express jquery Add Folders and Views Create a new folder named views in your project root, and inside it, create a file na...

How to Use AJAX with Node.js, Express, and MongoDB

Project Structure |-------------- models |-------------- task.js |-------------- public |-------------- data.js |-------------- routes |-------------- taskroute.js |-------------- views |-------------- demo.ejs |-------------- app.js |-------------- package.json Setup The Folder To create a folder, open your command prompt and type: mkdir ajax Change to the folder by typing: cd ajax Setup Node In Folder On the console, initialize your Node.js project: npm init -y This creates a package.json file indicating that Node is initialized in the folder. Install Packages Install the required packages for building the application: npm install body-parser express ejs mongoose jquery Models Create a new folder named models , add a file named task.js , and insert the following code: var mongoose = require('mongoose'); var taskSchema = new mongoose.Schema({ task:{ ...

How to Import CSV File Data into MongoDB Using Node.js and Express

Setup Folder Open your command prompt and create a new folder using the following command: mkdir csv After creating the folder, change into it by running: cd csv Setup Node In Folder Initialize the Node.js environment in your project: npm init -y This generates a package.json file containing the metadata for your application. Install Packages Install the required packages for handling CSV parsing, routing, template rendering, and file uploads: npm install csvtojson express ejs mongoose multer body-parser Add New Folders Create the following folders in your project root directory: models public views Models Folder (csv.js) Add a new file inside the models folder named csv.js to define your MongoDB collection schema: var mongoose = require('mongoose'); var csvSchema = new mongoose.Schema({ FirstName:{ type: String }, LastName:{ type: String }, SSN:{ type: S...

Adding Searching Functionality In Node

In this article we see how we can enable the searching of data from server in our node application. Setup The Folder  a) Make a new folder using command prompt.Type the following command. mkdir search b) change to the new folder cd search Setup Node In Folder For setting up node we type the following command npm init -y it will initialize the node in our project. And we get a package.json file in our folder. Which  means node is initialized in our folder. Install Packages Now we install the packages which will be required for building the application. npm install express ejs body-parser mongoose. Add New Folder models views models - this will contain our schema of collections(table). views - will contain the ejs pages.   Model Add new file in folder name it data.js Views Add 2 folders a)pages b)partial In partial folder add 2 new files header.ejs and footer.ejs header.ejs footer.ejs In pages add ho...

Hashing Password With Bcrypt In Node

Image
Image by Jae Rue from Pixabay Introduction In this article we will be using the bcryptjs javascript library for hashing and compare password.Here we will build a simple api fore register and login.when we will hash the password when user register and then compare that password with hash when they login. What is bcrypt ? bcrypt is a  password hashing function  designed by  Niels Provos  and David Mazières, based on the  Blowfish  cipher, and presented at  USENIX  in 1999.Besides incorporating a  salt  to protect against  rainbow table  attacks, bcrypt is an adaptive function: over time, the iteration count can be increased to make it slower, so it remains resistant to  brute-force search  attacks even with increasing computation power. What is hashing ?  Hashing is a one way function (well, a mapping). It's irreversible, you apply the secure hash algorithm and you cannot ge...

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. ...

How to Upload and Download Files in Node.js (Express & Multer)

Image
Here we will use multer middleware for uploading file . Click the link to know what is  multer . Setup The Project Folder create a new folder using console/terminal. open console/terminal type mkdir followed by the folder name you want to give. change to that folder by typing cd  folder name. Setup Node.Js For The Project now type npm init on console/terminal. the  npm init  will create a  package.json  file.But we can create it manually too. just open the folder and  add a new file  and name it  package.json  and then we can structure it the same way as its been structured when using  npm init . the package.json will look like this. now we will install packages for our project using npm install followed by package names we want to install. the following packages will be installed express ejs body-parser multer mongoose . this will generate a folder name node_modules which w...

CURD operations in node.

Image
Image by StartupStockPhotos from Pixabay Setup Node On System    If you dont have node installed on your system then you can download it   click here After installing the node on system.Open cmd and type node-v .It will give node version like this        Setup MongoDB On System  If you dont have mongodb installed on your system then you can download it from  here    Follow the installation instruction given there.   Now setup our node project  Open cmd type mkdir and give directory name. Now your directory to that using cd directory name which we created.  Now type npm init -y . this will make a package.json file in your folder. Now we will npm  install package which we will use in our project.And all these will be shown in package.json. The npm packages we will install here are express,mongoose,body-parser,ejs.          This is how pack...