Express - andrewkyllo-401-advanced-javascript/seattle-javascript-401d34 GitHub Wiki
Express Routing
Event driven system
app.get('/thing', (req,res) => {})
The Request Object
(req,..)
- /:parameters
app.get('/api/:thing',...) = req.params.thing
- Query Strings
http://server/route?ball=round = req.query.ball
- The Response Object
(...,)
- Responsible for sending data back to the browser
- Has methods like
send() and status()
Express Middleware
- A series of functions that the request "goes through"
- Each function receives
request, response, and next as parameters
- Application Middleware
- Error Handling
- Bringing in other routes
- Applies Defaults
- JSON, Body and Form Parsing
- Runs on every request
- Route Middleware
- Dealing with specific things for a route
- Routes participate in log states, IP address, first time user
Modularization & Separation of Concerns
- Modularizing your code into logical chunks
- Each thing should do the thing its best at
- Annoyance driven development
CRUD Operations with REST and Express
- CREATE
- READ
- UPDATE
- DESTROY
Server Testing
- Generally avoid starting the actual server for testing
- Export server as a module in a library
- Use
supertest to run your tests. It will mock your server
- You can wrap
superagent in a module
- Allows you to set up a mock of this new agent module
- Create a folder called
__mocks__ with the agent.js file in it.
- When you invoke
jest.mock() on the agent file in your test, jest is smart enough to use that mock's version instead of the real one
- You can fake every API call so that you are testing only the interface to the API, not the actual data
Test Pyramid
- Server Testing crosses boundaries
- Units: server internal functions
- integration: how it connects to other services
- really connect to other services
- acceptance
- The server might be a dependency of some other test