JumpStart – Building an Express server

 

Insomnia has an environment you can set baseurl and port
also sub environments

MIDDLEWARE

express has a built-in body-parser:
express.json uses body-parser as middleware
and support lotsa other middleware
https://expressjs.com/en/resources/middleware.html
each packet contains: header, data, checksum
https://en.wikipedia.org/wiki/Transmission_Control_Protocol

express doesn’t know how to read the body content so the header informs it of the content type, and it uses a parser to convert it to a JSON object

router is instance of app (has same methods), BUT
cannot be used in testing, use:

const app = require(“../../app”);

forms uses either:
application/x-www-form-urlencoded or multipart/form-data (for images)
sent through header as content-type

app.use(express.urlencoded()); to parse the form data

errorhandler middleware MUST take in 4 params:
https://expressjs.com/en/guide/using-middleware.html#middleware.error-handling
app.use(function(err, req, res, next)=>{})

res.json() both sets type and sends so no need for .send/.end

Leave a comment

Design a site like this with WordPress.com
Get started