import { express, middleware, NextFunction, Request, Response } from '..';
import { fsPath } from '../server/common';

export const router = express.router();

router
  // Add middleware
  .use(myMiddleware)
  .use(
    middleware.manifest({
      rootDir: fsPath.resolve('./src/example/manifest'),
      manifestPath: '/manifest',
      resourcePath: '/resource',
    }),
  );

/**
 * Custom express middleware.
 */
let total = 0;
export function myMiddleware(req: Request, res: Response, next: NextFunction) {
  if (req.method !== 'GET' || req.url !== '/middleware') {
    return next();
  }
  total += 1;
  res.send({
    msg: 'Middleware Response',
    now: new Date(),
    total,
  });
}
