Express query string check and send to client

I am currently working on project with express node.js to make web app.
Since I am new to express system and studying.. I need help!

I want to set route with query string and want to check if the query strings are validate to send.

For example, the query string would be like below(Has year, month)

https://localhost:3000/?year=2021&month=8

In that case, I want to compare the year / month date value is prior to the time of today. If today is 2021-08-31, then year should be equal or less than 2021 and month should be checked by year value.

If year and month query is not provided, then I want to add it and route to next.

I want to make some several route pages like below. Below is app.js file.

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var aRouter = require('./routes/a');
var bRouter = require('./routes/b');
var cRouter = require('./routes/c');
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', aRouter);
app.use('/b', bRouter);
app.use('/c', cRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

With above code, user can be routed to a, b, c router. Before routed to any url, the query string should be checked first. How can I do that simple?

Answer

You can achieve it by below reference code.

const router = require("express").Router()

router.get("/", (req, res, next) => {

    if (req.query.year && req.query.month){
        // your code for year and month verification
        return res.sendStatus(200)
    }
    return next()
})


module.exports = router