Express4 [TIL] 20201222 Express Router & Cookies Router An isolated instance of middleware and routes. Helps grouping routes into separate files. express.Router() Example //index.js const express = require('express'); const app = express(); const shelterRoutes = require('./shelters'); app.use('/shelters', shelterRoutes); //shelters.js const express = require('express'); const router = express.Router(); router.get('/', (req, res) => { //'/' is .. 2020. 12. 22. [TIL] 20201219 Handling Errors in Express Apps Express' Built-In Error Handler Responds with default 500 status code HTML response Custom Error Handlers Error-handling functions have four arguments (err, req, res, next) Example app.use(function(err, req, res, next) { console.error(err.stack); res.status(500).send('Something Broke'); //default error doesn't work anymore. } If you pass anything to the next() function, Express regards the curre.. 2020. 12. 19. [TIL] 20201218 Middleware: The Key To Express Express Middleware Functions that run during the request/response lifecycle. request → Middleware → response Each middleware has access to the request and response objects Middleware can end the HTTP request by sending back a response with method like res.send() Or middleware can be chained together, one after another by calling next() Middleware functions can perform the following tasks: Execut.. 2020. 12. 19. [TIL] 20201216 Express / Dynamic HTML with Templating / RESTful Routes Express An NPM package which comes with a bunch of methods and optional plugins that we can use to build web applications and APIs Fast, unopinionated, minimalist web framework for Node.js Start up a server to listen for requests Parse income requrests Match those requests to particular routes Craft our http response and associated content Library vs. Framework Library: The user controls the flo.. 2020. 12. 16. 이전 1 다음