- Create a simple server with JS, NodeJS and express
- Following the tutorial below
- The strcuture of an express application
- HTTP methods GET POST PUT DELETE
- REST api endpoints
- CRUD create read update delete
- Testing our endpoints with Postman
- How to use middleware
- body parser --> process incoming data such as body payload - data received from client
- session --> a session is often used in applications such as login/signup
- time logger --> logs the time/date of each route change
- can be used on specific routes
- Assignment create a simple registration and landing page
- Path params and Query strings
- An example of path params & query strings at work
- Getting HTML files
- getting route for a file in the same directory
- [__dirname] specifies the folder
- on the form element we need the attributes action="/PATH" and method="post"
- write a simple PUG page
- URI (uniform resource identifier - name, location of both)
- URL - uniform resource locator
- URN - uniform resource name
- Pug - formaerly known as Jade
- indentation mattersalot in Pug -> take care and be mindful
- a node package that can be installed -> npm install pug
- setup and usage of Pug
- create files in Pug -> layout and registration
- Pug converters -->
- Adding images, js and css to our pug files
- app.use(express.static(path.join(__dirname, 'public')))
- this loads any images, css, js and other files you may want in your project
- it sets the default directory for all these files in the public directory
- no need o reference the directory when linking css or images for examples
- app.use('/public/images', express.static(__dirname + '/public/images'))
- handles images you upload
- installation of the following dependecies into our project
- npm install connect-ensure-login
- npm install express-session
- npm install multer
- npm install mongoose
- npm install passport
- npm install passport-local
- npm install passport-local-mongoose
- npm install moment
- Assignment: Read about the MVC (model views controller) structure
- Assignment: Add more Pug files to your project
- Push your files to GitHub
- Share the link to the repo
- Appyling the MVC architecture in our project
- What is a mdolue?
- Setup the router file, exported it
- const express = require("express");
- const router = express.Router();
- Created a variable in the server file to access our routes
- const registrationRoutes = require("./routes/regRoutes");
- Middleware that allows us to acces the routes in the router file in the browser
- app.use("/user", registrationRoutes);
- Database Creation
- Schema / Document / Collection
- Databases
- Import mongoose into the server -> this helps us to connect to mongodb without constantly opening it
- Create a config dir and a db.js file
- then import the db file into the server
- setup db connections in server
- import passport into server and add middleware
- passport will help with us authentication
- and tracking authentication in our system
- import express-session and its middleware intot the server
- Models (MVC)
- Create a user registration schema in your models file
- a schema sets the design of your regstration in the db
- see What is a schema?
- see What is a collection?
- see What is a db/ collection/ document?
- Learning about JS Async/Await, Callbacks, Promises by making ice cream -->
- Assignment (see google classroom)
- check for users with existing user id, to stop duplicates
- how to add routes for another file
- how to get the list of registered users (specifically farmer one)
- create a schema for produce upload with provision for owner of the produce
- create a produce upload route
- handles image uploads with multer
- uses aycn await in routes
- the commented route was checking for registered FOs and the rendering the upload page
- the post route was saving the file path of a single uploaded image and redirecting back to the dashboard
- Assignment (see google classroom)
- Created authentication routes for our login/logout
- Used CONNECT-ENSURE-LOGIN on the get route for produce uploads - cannot upload w/o being logged in
- connect-ensure-login tracks users login sessions
- Addes a logout button on the landing page that destroys a users login session and redirects back to the login page
- The logout btn is a mini form that can be added to any page where you want a user to be able to logout
- [status: {
type: String,
default: 'pending',
enum: ['pedning', 'approved'],
}]
- find out what this does
- [xxx: {
type: mongoose.Schema.Types.ObjectId,
ref: 'RegisterUser',
required: true]
- find out what this does
- Assignment --> create dashboards for the respective users AO, FO, UF (see more on the Google Classroom)