express#IRouter TypeScript Examples

The following examples show how to use express#IRouter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: chargepoint.ts    From scriptable-ocpp-chargepoint-simulator with Apache License 2.0 6 votes vote down vote up
/**
   * Starts a web listener (aka web server), usually used in batch mode only. Use the returned IRouter to add routes. Also the returned IRouter
   * instance has a method 'terminate()' to gracefully shutdown the web server.
   *
   * @param port to bind
   * @param bind address, default localhost
   * @param users when given basic authentication is enabled with user: password
   */
  startListener(port: number, bind?: string, users?: { [username: string]: string }): IRouter {
    const expressInit = express();
    expressInit.use(express.json());
    const server = http.createServer(expressInit);
    server.listen(port, bind);
    server.on('error', (error: NodeJS.ErrnoException) => {
      console.error(error);
    });
    server.on('listening', () => {
      const addr = server.address();
      log.debug(LOG_NAME, this.config.cpName, `Listening on ${JSON.stringify(addr)}`);
    });
    const httpTerminator = createHttpTerminator({server})
    expressInit['terminate'] = (): void => httpTerminator.terminate();
    if (users) {
      expressInit.use(expressBasicAuth({users}));
    }
    return expressInit;
  }
Example #2
Source File: endpoint-server.ts    From server with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Creates a Route Containing all the Endpoints in the Server
   *
   * @param {Response} res - Express Response Object
   * @param {IRouter} router - Express Router Object
   */
  constructor(res: Response, router: IRouter) {
    this.response = res;
    this.router = router;
  }
Example #3
Source File: setup.ts    From server with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Serves the Purpose
   *
   * @returns {IRouter} - Router
   */
  serve(): IRouter {
    this.router.post('/get', (async (req, res) => {
      try {
        const leanHeader = req.headers['x-lean-doc-request'];
        const leanRequest = leanHeader ? true : false;
        const frontendDocs = await this.model.find({}).lean(leanRequest).exec();
        okResponse(res, frontendDocs);
      } catch (e) {
        errorResponseHandler(res, e);
      }
    }) as RequestHandler);

    this.router.delete('/reset', (async (req, res) => {
      try {
        const result = await this.model.clearAll();
        okResponse(res, result);
      } catch (e) {
        errorResponseHandler(res, e);
      }
    }) as RequestHandler);

    return this.router;
  }
Example #4
Source File: ExpressReceiver.d.ts    From flect-chime-sdk-demo with Apache License 2.0 5 votes vote down vote up
router: IRouter;
Example #5
Source File: endpoint-server.ts    From server with GNU General Public License v3.0 5 votes vote down vote up
router: IRouter;
Example #6
Source File: setup.ts    From server with GNU General Public License v3.0 5 votes vote down vote up
router: IRouter;
Example #7
Source File: get-all-routes.ts    From server with GNU General Public License v3.0 5 votes vote down vote up
getRoutes = function (app: IRouter): Routes {
  const routes: Routes = {
    get: [],
    post: [],
    put: [],
    patch: [],
    delete: [],
  };

  const processMiddleware = (middleware: any, prefix = ''): void => {
    if (middleware.name === 'router' && middleware.handle.stack) {
      for (const subMiddleware of middleware.handle.stack) {
        processMiddleware(
          subMiddleware,
          `${prefix}${regexPrefixToString(middleware.regexp)}`,
        );
      }
    }

    if (!middleware.route) {
      return;
    }

    const { method } = middleware.route.stack[0];
    const { path } = middleware.route;

    switch (method) {
      case 'get':
        routes.get.push(`${prefix}${path}`);
        break;
      case 'post':
        routes.post.push(`${prefix}${path}`);
        break;
      case 'put':
        routes.put.push(`${prefix}${path}`);
        break;
      case 'patch':
        routes.patch.push(`${prefix}${path}`);
        break;
      case 'delete':
        routes.delete.push(`${prefix}${path}`);
        break;
      default:
        throw new Error(`Invalid method ${method}.`);
    }
  };

  for (const middleware of app.stack) {
    processMiddleware(middleware);
  }

  return routes;
}