ExpressJs

Basic Setup

const express = require('express')
const app = express()

// Setup static and middleware
app.use(express.static('./public'))

app.get('/', (req, res) => {
	res.send('Home Page')
})

app.all('*', (req, res) => {
	res.status(404).send('<h1>Page not found</h1>')
})

app.listen(5000, () => {
	console.log('Server is listening on port 5000...')
})

API vs SSR

API
SSR

JSON

Template

Send Data

Send Template

Res.JSON()

Res.Render()


JSON APIs


Middlewares

  • These are functions that gets executed during the request to the server

  • Has access to request and response object

  • We must pass on the request to next middleware

Method 1

Method 2

middleware.js

app.js

Types

  1. Own

  2. Express

  3. Third Party

    1. Morgan (Logger)


POST Method


Express Router

people.js

auth.js

app.js


Express Router Controllers (MVC)

people.js

people-router.js


Mongoose

  • Mongodb object modelling library for node.js


Last updated