SALAD ORDER TRACKER APPLICATION
- A real time application to track orders online. Users can order Salads, view cart, and check the delivery status.
- Part of 100 days of code challenge.
Technology Stack
- JavaScript - NodeJS Framework
- ExpressJS
- EJS Template
- YARN (package manager)
- MongoDB
- Laravel Mix
- Tailwind CSS
- Socket.io
- ES6
- SCSS
Project Structure - MVC pattern
Model
is the Database part of the application. It represents the structure of data, the format and the constraints with which it is stored.
View
is the User Interface part. It utilizes the Model(using controllers) and presents data as static/dynamic pages to the user for further tasks.
Controller
is the request-response handler. The user interacts with the View, which in turn generates a request, that will be handled by a controller. The controller renders the appropriate view with the model data as a response.
Installations
- Download & Install NodeJS
- Download & Install YARN
- Download & Install GIT
- Steps for Installing Laravel
- Using Tailwind CSS via CDN (No need to install complete framework)
- MongoDB for Windows
- MongoDB Compass
YARN Commands
- yarn -v (check version of YARN)
- cd realtime-salad-app (to work in the project folder)
- yarn init (Initializes package.json file in root of project)
- yarn add express ejs express-ejs-layouts (The above command performs 4 tasks-)
- Writes dependencies to package.json,
- Downloads express, ejs and express-ejs-layouts dependencies from there respective repositories on the Cloud,
- Adds node_modules folder, which contains all the dependencies like a Tree structure, and
- Creates yarn.lock file, which locks down the versions of the dependencies specified in the package.json file.
- yarn add nodemon -D (downloads nodemon that automatically restarts the server on any changes in the project & adds nodemon to devDependencies in package.json)
- yarn dev (runs the server)
- yarn add laravel-mix -D (downloads Laravel Mix, updates node_modules & adds laravel to devDependencies in package.json)
- yarn add cross-env --save-dev (to handle different environments)
- yarn watch (performs the following tasks -)
- installs sass, sass-loader & resolve-url-loader dependencies when executed first time, and saves to package.json.
- compiles SCSS & JS files using laravel-mix, and stores the compiled code to public folder i.e. CSS & JS.
- yarn add mongoose (helps in interaction of JS code with MongoDB)
- yarn add express-session (helps in storing sessions in key-value form)
- yarn add dotenv (a zero-dependency module that loads environment variables from a .env file into process.env)
- yarn add express-flash (used to flash messages for the application)
- yarn add axios (promise based HTTP client for the browser and node.js)
- yarn add noty (notification library that makes it easy to create alert, success, error, warning, information and confirmation messages)
- yarn add bcrypt (A library which helps in hashing passwords)
- yarn add passport passport-local (Authentication)
NPM Commands
NPM package manager is automatically downloaded with NodeJS, and it can be used as an alternative to YARN. Since NPM has a drawback that it works quite slow when compared with YARN (as YARN installs dependencies simultaneously), I will be using YARN in this project. However, I am providing NPM commands as well for reference.
- npm -v
- cd realtime-salad-app
- npm init
- npm install express ejs express-ejs-layouts
- npm install nodemon --save-dev
- npm run dev
- npm install laravel-mix --save-dev
- npm install cross-env --save-dev
- npm run watch
- npm install mongoose
- npm install express-session
- npm install dotenv
- npm install express-flash
- npm install axios
- npm install noty
- npm install bcrypt
- npm install passport passport-local
GIT Commands
1\. git --version (checks version)
2\. git init (creates .git file )
3\. git status (to check if files are committed or not)
4\. git add . (adds all files, ready to commit)
5\. git commit -m "commit message here"
6\. git push origin master -f (pushing all changes to github; -f stands for forced push)
7\. git status (recheck files)
8\. [.gitignore commands](https://stackoverflow.com/questions/12501324/how-to-use-gitignore-command-in-git)
9\. [.gitignore for NodeJS guide](https://github.com/github/gitignore/blob/master/Node.gitignore)
Why use Laravel Mix?
- A tool used to compile resources like modern JavaScript and SCSS code and store it to the public folder in JS and CSS format.
- Advantage over other tools - avoids manual configuration (internally uses WebPack).
- Works inside Laravel framework usually, but has an benefit that it can be used in Stand Alone projects like this one.
- Laravel Guide
webpack.mix.js
- A configuration file downloaded to node_modules while installing Laravel Mix.
- Copy this file from node_modules to the root of project directory using the command given below.
- cp node_modules/laravel-mix/setup/webpack.mix.js ./ (use copy in case of Windows OS)
- This file imports laravel-mix module, and uses the methods js() & sass() for compilation and then storing of the compiled files.
Scripts in package.json
- Update scripts in package.json file to compile these files.
- The watch command automatically compiles the file everytime it is changed, eliminating the need to manually compile repeatedly.
- cross-env : Runs Scripts that set and use environment variables across platforms (useful in production).
layout.ejs
- An ejs file that serves the common content such as header, footer and nav bar to all the pages in the application.
- Code in other ejs files like home, login and register, is simply embedded using
<%- body%>
after the nav bar.
Session Configuration
- Done using express-session module, needed to store Cart data in this project.
- The server creates & stores a session ID (sid) when a new client Requests for the web page.
- Then it sends a message in Response header, which has sid, asking the client to create a cookie using that session id.
- For every next request header from client, the cookie will be send to server, so that the server can recognize the client as an existing one.
Express Methods (Assets)
Built-in middleware functions in Express.
Database Connectivity - MongoDB, NodeJS & Mongoose
- Mongoose is an Object Data Modeling library, that provides object mapping between MongoDB and NodeJS.
- Connection is established using
mongoose.connect()
for single connection, or mongoose.createConnection()
for multiple connections.
- A Schema helps in defining the fields stored in each document along with their validation requirements and default values.
- The timestamps parameter helps to add createdAt and updatedAt properties automatically to the particular document in the collection.
- Schemas are then “compiled” into Models using the mongoose.model() method.
const MyModel = mongoose.model('ModelName', mySchemaName);
- The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural version of your model name.
- Mongoose exists() method
- Mongoose findById() method
- Mongoose findOne() method
Hashing Passwords using bcrypt library
Using bcrypt is a secured way to store passwords in a database regardless of whatever language the app’s backend is built in — PHP, Ruby, Python, Node.js.
const bcrpyt = require("bcrypt")
const hashedPassword = await bcrpyt.hash(password, 10)
Passport - authentication middleware for Node.js
- passport-local is a passport strategy for authenticating with a username and password.
- Usage/Configuration
- Middlewares
- passport.initialize()
- passport.session()
- Passport will serialize and deserialize user instances to and from the session using:
- passport.serializeUser()
- passport.deserializeUser()
Resources
Download Details:
Author: VertikaJain
Source Code: https://github.com/VertikaJain/realtime-salad-app
#node #nodejs #javascript