Please enable JavaScript.
Coggle requires JavaScript to display documents.
BASIC - Coggle Diagram
BASIC
SENDING
res.
send()
For-Testing
sendStatus(..)
200/400/500
Sending-StatusCode
status(code)
.send(data)
.json(..)
json({..})
download(filePath)
render(..)
Args
[dataObjForView]
htmlFilePath
Send-HtmlMarkup
redirect(path)
Redirect-ToURL
SETUP
Install
Node
Initialize
npm init -y
Libraries
--save-dev
nodemon
dotenv
Normal
express
Scripts
Add
In-package
devStart : "nodemon server.js"
Use
npm run devStart
server.js
Create
Proxy
npm config
set proxy/https-proxy
http://user:pass@host:port
SERVER.JS
const express
= require('expess')
const app
= express()
app.listen(3000)
Request
Types
app
.put/patch
get/delete
post
Routes
app.get(path, func)
func(...)
args
res
[next]
99.9%
Not-used
req
doSomething
res.send('text')
VIEW-ENGINE
Using
In-Template
<%= 2+2 %>
Renders-4
Output-ToPage
%=
Access
Field-Of
Passed-Obj
<%= fieldName %>
As-Locals
locals.fieldName
Prevent
Undefined-Err
<%= locals.fieldName
|| "DefaultVal" %>
RENDERING
Views
Are-Html
Files
Create
views/index.ejs
View-Engines
ejs
npm i ejs
pug
Using
After
App-Declare
app.set('view engine', 'ejs')
Extension
ejs-lang-support
ROUTERS
Advanced
Get-By-Id
router.get(..)
'/:id'
func(req,res)
const id =
req.params.id
Put-Dynamic
After
Static
Chaining
Removing
Duplicacy
router.route('/:id')
.get(func)
.put(func)
.delete(func)
Basic
Create
routes/users.js
const express
= require('express')
const router
express.Router()
Methods
Similar-ToApp
router.get/post
Export
module.exports
= router
server.js
Before-listen
const userRouter
= require('./routes/users')
app.use(..)
/users
userRouter