Skip to content

ExpressJS

Posted on:September 23, 2022 at 03:22 PM

Library vs framework

Planning a Web project

Express

Get vs Post request

Passing variabler to ejs template

Handling variable paths

Handling get request from forms in express

Handling post request from forms in express

Handling posting of json data

Enable PUT and PATCH verbs in the forms

Redirect

What is REST

  1. Client–server architecture
  2. Statelessness: no session information is retained by the server. Every http reuest is independent.
  3. Cacheability: clients and internediaries can cache data. Response should contain info if it is cachable or not.
  4. Layered system: There can be proxies and load balancers between client and server. They are not visible to client.
  5. Code on demand (Optional): Client side scripts, server can transfer executable scripts to the clients.
  6. Uniform interface

RESTFul routes

DescriptionRouteMethod
Display all commentsGET/comments
Display details of a commentsGET/comments/:id
Add a commentPOST/comments
Form to add new commentGET/comments/new
Update a commentPUT/PATCH/comments/:id
Form to update a commentGET/comments/:id/edit
Delete a commentDELETE/comments/:id

Mongo DB

BSON

Inserting into mongo db

  1. db.collection.insertOne(): Inserts a single document into a collection.
  2. db.collection.insertMany(): inserts multiple documents into a collection.
  3. db.collection.insert(): inserts a single document or multiple documents into a collection.

Quering mongo db

Updating mongodb

db.restaurant.updateOne(
      { "name" : "Central Perk Cafe" },
      { $set: { "violations" : 3 } }
   );

Deleting in mongo db

 db.orders.deleteOne( { "_id" : ObjectId("563237a41a4d68582c2509da") } );

delete everything in a collection

Additional mongo operators

Mongoose

Mongoose model operations

Creating mongoose schema

Mongoose instance methods

Mongoose static methods

Mongoose virtuals

Mongoose middleware

express middleware

protecting a specific route

Data validation library Joi.js

mongodb relations

ejs tool (ejs-mate)

handling errors in express

Mongoose middleware

Express router

HTTP/Web Cookies

HTTP sessions

flash

Security

Authentication vs authoriation

Hashing

MVC pattern

Submitting image file via form

mapbox

Security

Storing API keys and secret info in the app

Deploying express app on Heroku

  1. Heroku login.
  2. Git status → git init.
  3. Git add filename.
  4. Git commit -m “commmit message”
  5. Heroku login.
  6. Heroku create.
  7. Git remote -v.
  8. git push heroku master.
  9. Heroku logs (shows logs for app in current dir)
  10. we can run any command (ls, npm etc) using.
$Heroku run bash-command

mlabs

To connect using the mongo shell:

$ mongo ds135003.mlab.com:35003/yelpcampsp -u <dbuser> -p <dbpassword>

Heroku password : S******3!

To connect using a driver via the standard MongoDB URI:

//saurabh:Se*****3!@ds135003.mlab.com:35003/yelpcampsp
mongodb: app.listen(process.env.PORT, process.env.IP);

App deployed here