Matthew Bodily
Lecture Slides for DevMountain's Web Development Course
Top level middleware is functionality that runs on every request received by your server.
The prime example of this is your JSON parser.
app.use(express.json())
When working with Express, top level middleware can be created by using the use( ) method, seen above.
CORS stands for Cross Origin Resource Sharing. Your CORS settings dictate whether other servers and clients from a different domain can communicate with your own server.
By default, your Node/Express doesn't allow CORS, but we can allow it by installing the 'cors' package from NPM.
npm install cors
Once installed, require cors at the top of your server and then invoke it as top level middleware.
const express = require('express')
//require cors
const cors = require('cors')
const app = express()
//top level middleware
app.use(cors())
Postman is an application that is used to test server endpoints.
To get started with Postman, use the link below to install it, then start the application and follow the set-up.
To use Postman, select the type of request (get, post, etc.) and then specify the URL for the request.
Hit send to see your results.
Postman also allows you to import collections.
Collections are a group of requests that allow you to test multiple endpoints at once.
You will use collections on your mini and afternoon projects today.
To use the collections, click import and select the 'postman_collection' folder sitting in your project directory.
Once imported, click on the runner button to open the test runner. Click 'Start Run' when you're ready to test your endpoints.
Controller files are a great place to separate logic from your server. (Remember the Modular Pattern?)
//controller.js
module.exports = {
handlerFunc: (req, res) => {
//functionality here
}
}
module.exports allows our functions to be exported as an object
handler function
require controller
access function
//index.js
const ctrl = require('./controller.js');
app.get('/example', ctrl.handlerFunc)
By Matthew Bodily
Controllers, Middleware, and Postman