Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8498049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:17:47+00:00 2026-06-11T00:17:47+00:00

I am using latest versions of NodeJS and ExpressJS (for MVC). I usually configure

  • 0

I am using latest versions of NodeJS and ExpressJS (for MVC).

I usually configure my rest paths like this, for example:

app.get('/archive', routes.archive);  

Now i want my /admin/* set of URLs to be secured, I mean I need just simple authentication, it’s just a draft.

When a user tries to access, for example, /admin/posts, before sending him the corresponding view and data, I check for a req.session.authenticated. If it’s not defined, I redirect to the login page.

Login page has a simple validation form, and a sign-in controller method: if user does send “right user” and “right password” I set the session variable and he’s authenticated.

What I find difficult, or I don’t understand, is how to actually make the “filter” code, I mean, the auth check, before every /admin/* path call.

Does this have something to do with “middleware” express functions?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:17:49+00:00Added an answer on June 11, 2026 at 12:17 am

    Yep, middleware is exactly what you want. A middleware function is just a function that works just like any other Express route handler, expept it gets run before your actual route handler. You could, for example, do something like this:

    function requireLogin(req, res, next) {
      if (req.session.loggedIn) {
        next(); // allow the next route to run
      } else {
        // require the user to log in
        res.redirect("/login"); // or render a form, etc.
      }
    }
    
    // Automatically apply the `requireLogin` middleware to all
    // routes starting with `/admin`
    app.all("/admin/*", requireLogin, function(req, res, next) {
      next(); // if the middleware allowed us to get here,
              // just move on to the next route handler
    });
    
    app.get("/admin/posts", function(req, res) {
      // if we got here, the `app.all` call above has already
      // ensured that the user is logged in
    });
    

    You could specify requireLogin as a middleware to each of the routes you want to be protected, instead of using the app.all call with /admin/*, but doing it the way I show here ensures that you can’t accidentally forget to add it to any page that starts with /admin.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a simple application using the current latest versions of nodejs +
I'm using derived tables (latest version of MySQL) to get counts and data from
I'm using the latest versions (jQuery 1.7.1 and jQuery UI 1.8.16) of all libraries,
So I've got the latest versions of jQuery and UI running. I'm using the
I'm trying to upgrade my WinForms app to the latest versions of NHibernate and
Here is my code: jsFiddle . I've tested this in the latest versions of
Could someone explain why both tests using the latest versions of Moq and Rhino.Mocks
I am using the latest versions of MySQL (workbench), Perl, and DBI. I have
I'm using the latest versions of knockout.js and jquery. I'm trying to create a
Using the code below, both Chrome and Opera (latest versions supporting border-radius) on Mac

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.