Express app.get documentation

前端 未结 1 479
南旧
南旧 2020-12-24 07:40

I am looking for some documentation on the app.get function of express.js.

app.get(
    \'/path\', 
    middleware(),
    function(req, res) {
          


        
相关标签:
1条回答
  • 2020-12-24 08:45

    The docs for that are part of the app.METHOD documentation, where get is one of the supported HTTP methods.

    The second, optional parameter, is called middleware (and you can pass an array of middleware functions). This is a function that's called before the third parameter callback (the actual route handler) and the responsibility of a middleware function is to allow your code to follow the DRY (don't repeat yourself) principle.

    Example of middleware functions are permissions checks, access validations, validation of sessions (if user is not in logged in, take him to a log in page), and such.

    Since several routes might desire the same behavior, you use a middleware so that you don't have to write the same code several times.

    0 讨论(0)
提交回复
热议问题