DEV Community

Zach
Zach

Posted on

Git and Middleware

In this post:

git

This is actually a piece that I shared first with my teammates and then again with my cohort-mates over Slack.

A small problem-solve of mine today:

  • The problem
    • Remote branch has one whole directory that I’d like to pull into clean main branch.
  • Bad Solution
    • git merge <BRANCH>
    • Why? Merge conflicts! Why deal with conflicts when I know my target directory to be pulled in is 100% clean.
  • Good Solution
  • 'git checkout '
    • Why? This will merge/overwrite/import (whatever you want to call it) anything standing in the way of moving exactly what you want, exactly where you want it. It’s brute force and this is a time that calls for force.
  • Further Reading: https://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/

Express Middleware:

I have a little bit of experience with middleware from working with Django. Seeing it again in Express has been very consonant. The way I see it from each tool is that middleware concerns attaching processing to requests as the request travels from the user, through the server and its middleware chain, and then back to the user in the form of the response.

In Django, middleware is applied to the request object, which natively contains a ton of information including user data.

I've been using openId to manage authentication for these very early stages of my blog project. Express applies middleware app-wide via the app.use() function. To attach user data to requests, you call .use on some authentication config data and then Express tracks that data across the application.

To login-protect a route in express you can apply the requiresAuth() function as a parameter to the http request function like so:

app.get('/profile', requiresAuth(), (req, res) => {
  res.send(JSON.stringify(req.oidc.user));
});
Enter fullscreen mode Exit fullscreen mode

I never wrote custom middleware for Django. As I grow as a developer, I'll look to leverage middleware to make my request handling more dynamic.

In the same way I look forward to applying methods to models on Mongoose to make code both DRYer and more flexible.

Top comments (0)