Open Terminal and type
node -v
Prefered 18 or highernpm - v
Computer / Mobile -> request -> [Express] -> Server
Computer / Mobile <- [Express] <- Response Server
-
Task :-
-
Go to any url say https://www.postman.com/ ; what happens here is
-
server Listens to the request , and server decides response
-
All the listening work is done by package
-
Route terms :-
- visiting https://www.postman.com/ means you are visting home route ; represented by
/
- visting https://identity.getpostman.com/#means you are visting
/#
, entering user details setup
- visiting https://www.postman.com/ means you are visting home route ; represented by
-
Types of request:-
-
: basic requesting ; visting any url ; to get the service from the server of that url website
-
-
-
Make a new folder say Chaideploy in VSCode and open terminal
- Make an empty Node application . How ?
- simply
npm init
in terminal-
:- do npm init -y
only when yes to all defaults
-
- observe the output
- simply
- fill the needed ; leave the rest to default by just clicking ENTER to next
- entry point :(index.js)
- test command :
- git repository:
- keywords : node chai
- author : YourName(anything)
- license:(ISC)
- You will get a summary of all filled details ;
- Now check the automatically generated file :
- Make an empty Node application . How ?
-
Make a new file
- to test write
console.log("Hello World")
- run by typing
node index.js
in terminal
- to test write
-
But How to run using package.json ?
- use
commands - go to package.json and modify the code for scripts
"scripts" :{ "start" : "node index.js" }
-
: - common approaches are build , dev
-
- But will it do ; when we run
npm run start
- actually command run will be
node index.js
- observe that terminal terminates after running the file
- actually command run will be
- use
-
Installing
- Go to official docs of express ; search in browser
- simply run
npm install express
in terminal
- simply run
- check package.json if installed
- visit getting started , Hello world section in docs
- Go to official docs of express ; search in browser
-
Code part
To be written in index.js file
const express = require('express') const app = express() const port = 3000 app.get('/' , (req , res) =>{ res.send("Hello World") }) app.listen(port , ()=>{ console.log("Example app listening to port ${port}") })
-
Code Explaination
-
const express = require('express')
- Style of writing : common vs modulejs
- importin package ; use as it is
-
const app = express()
- app is a object like Math in JS ; we use Math.random()
-
const port = 3000
- There are about 65K + available virtual ports that are free to use
- listening to server
- Not fixed ; can use 2000 or 4000 too
-
app.get('/' , (req , res) =>{ res.send("Hello World") })
/
: Denotes Home Route- At this home route listen to the request , we will give a callback , sending message using
res.send()
:- response - can we handle more requests like these? Yes
-
app.get('/twitter' , (req , res) =>{ res.send("Hello World") })
'/twitter'
:- it is route(req , res) =>{}
: - callback
-
-
app.listen(port , ()=>{ console.log("Example app listening to port ${port}") })
if succesfully listening we get a this Message response in terminal
-
- run in terminal
npm run start
- 📝: Terminal didnt terminated ; means that app is listening to the requests constantly
- 🎉 Congrats!! You made a server
- How to visit the url
- go to http://localhost:3000 or 127.0.0.1:300
- depending on the localhosting url of pc
- run in terminal
-
-
app.get('/#' , (req , res)=> { res.send('<h1>Please Login </h1>) })
- write in index.js file
- run at http://localhost:3000/#
- Problem occurs ; cannot GET/# at the page ,What and how ?
- Concept of Hot Reloading ;in react app , every change is saved and reloaded automatically But here we have to reload manually
- Solution restart the application by :-
- ctrl+c/break in terminal
npm run start
again
-
-
-
app.get('/youtube' , (req , res)=> { res.send('<h2>Welcome to my yt channel</h2>) })
- restart the app and see the output
-
We get some special variables which need to be taken care of ; like sensetive info , Db username .Also the port assigned in our pc works fine but who knows it is free or not on client
- install dot env ; go to Docs
npm i dotenv
run in terminal- Make a new file .env and write
PORT= 3000
- standard practice to write in capitals
- How to use ;
- At the start of index.js add at the top
require('dotenv').config()
- Also change
-
app.listen(process.env.PORT , ()=>{ console.log("Example app listening to port ${port}") })
-
- At the start of index.js add at the top
- run the application and see the changes
- Go to github
- create a new repositary say chaibackend
- Add description
- ✅ public and create
- if not done yet , do git ssh setup to configure your pc
- In terminal run the following commands
git init
-
: Make a .gitignore file and add - node_modules
- .env
git add .
-
: Way to visulize , git section left sidebar git commit - m "first commit"
git branch -M main
-
git remote add origin repo_link
: add repo_link created git push -u origin main
-
Deployment Alternatives
DigitalOcean , Heroker , railway , seanode , render , cyclic
-
Steps to follow
-
resource :- Github
- check repo , branch and source directory
- Auto refresh
-
Edit Plan : Basic
-
Env Variables:
- global and repo name both write ; both have difft scope
-
key value PORT 3000 - for system security
- port is overwritten automatically; DB part will not
-
Check info summary
- go to build logs to check live progress
- check apps : - url
-
-
Wtite to index.js
-
const githubData = 'https://api.github.com/users/{GithubName}'
- replace your {GithubName} with your github name
-
-
To production just push to github
- select build in app
-
: cmd - console features; insights ; console ; activity
-
Destroy app in Actions
- node_modules
- .env
- .gitignore
- index.js .package.json .package-lock.json