Giter Site home page Giter Site logo

middleware_intro's Introduction

Middleware

Intro to building middleware for ExpressJS.

app.use will run for every single req (wether it's a GET, POST , etc). If it returns, nothing will run after it, so it's important that if we want our program to keep running, to put a next() at the end so it know to move on to the next middleware.

This can be useful, for example, when you want to use middleware for a group of internal routes or authenticated routes. It's also useful to create 404 pages.

Using middleware to protect routes

Because middleware executes for every single req, we can use it to protect routes. Basically creating functions that check for certain parameters in order to grant access to that route (by allowing the next() command to run if checks pass).

IMPORTANT - This is NOT how you do proper authentication, this is just an example of using middleware to protect routes. In other words, NEVER send a password as a query string.

Example - This protects every single route

app.use((req, res, next) => {
    const { password } = req.query;
    if(password === 'chickennugget'){
        next();
    }
    res.send('Sorry, you need a password')
})

Protecting specific routes

Instead of passing the authentication middleware through app.use as in the previous example, we can actually pass it to the req. When we define a path, i.e. app.get , we pass in a path and we can optionally pass multiple callback functions. These callback functions will behave just like middleware.

const verifyPassword = (req, res, next) => {
    const { password } = req.query;
    if(password === 'chickennugget'){
        next();
    }
    res.send('Sorry, you need a password')
}

app.get('/secret', verifyPassword, (req, res) => {
    res.send('My secret is: Sometimes I wear headphones in public')
})

middleware_intro's People

Contributors

mayanhavoc avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.