Home > Software design >  Combine non ssr routes to avoide repeated code Angular SSR server.ts
Combine non ssr routes to avoide repeated code Angular SSR server.ts

Time:12-28

In my server.ts I have seperated some url that needs to be served as non ssr. This seems working fine but I have few other url that needs to be added. I have to clone the three lines of code and replace that route for another non ssr. Is there a way I can combine those url as an array so I do not have to repeat code.

My server.ts

  server.get('/invite', (req, res) => {
    console.log('invite');
    res.sendFile(distFolder   '/index.html');
  });
  server.get('/invite/**', (req, res) => {
    console.log('invite/**');
    res.sendFile(distFolder   '/index.html');
  });
  server.get('/dashboard', (req, res) => {
    console.log('dashboard');
    res.sendFile(distFolder   '/index.html');
  });
  server.get('/dashboard/**', (req, res) => {
    console.log('dashboard/**');
    res.sendFile(distFolder   '/index.html');
  });
  server.get('/public/**', (req, res) => {
    console.log('public/**');
    res.sendFile(distFolder   '/index.html');
  });
  server.get('/pre', (req, res) => {
    console.log('pre/**');
    res.sendFile(distFolder   '/index.html');
  });
  server.get('/public', (req, res) => {
    console.log('public**');
    res.sendFile(distFolder   '/index.html');
  });
  
  // All regular routes use the Universal engine
  server.get('*', (req, res) => {
    res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
  });

CodePudding user response:

Well, you can pass just an array to the get method.

 server.get([
  '/invite',
  '/invite/**',
  '/dashboard',
  '/dashboard/**'
  ], (req, res) => {
    console.log('Called path', req.route.path);
    res.sendFile(distFolder   '/index.html');
  });
  • Related