1

I have developed an Angular Universal project. All pages are rendered using express like this:

server.get('*', (req, res) => {
    res.render(indexHtml, { req, providers: [
            { provide: href, useValue: req.baseUrl },
            { provide: REQUEST, useValue: req },
            { provide: RESPONSE, useValue: res }
        ]});
});

But, I have also some personalized URLs, that I don't want to prerender, I just want that express respond simple static index file, which browser will load. I was trying like this:

server.get('/personal-information', express.static(indexHtml));

But no result. Please help.

  • What's the issue your facing, what do you get as response? Is it the regular one instead of `/personal-information` config? –  Jun 05 '20 at 09:46
  • @PraveenThirumurugan the issue is that these pages are still rendering on server side, but i do not want this – Kateryna Makieieva Jun 05 '20 at 12:37

2 Answers2

0

express.static() expects the first parameter to be a path of a directory, not a filename. I would suggest creating another subdirectory to contain your index.html and use that. Refer to this answer.

So if you have something like:

  server.get('*.*', express.static('public'));

You need to create a folder called personal-information inside your public path and put your index.html there.

Santi Barbat
  • 2,097
  • 2
  • 21
  • 26
0

You can also parse the URL like this in your code.

server.get('*', (req, res) => {
    const params = req.params[0].split("/");
    if (params[1] && params[1] === "personal-information") {
        res.send(indexHtml);
    }
    else {
        res.render(indexHtml, { req, providers: [
            { provide: href, useValue: req.baseUrl },
            { provide: REQUEST, useValue: req },
            { provide: RESPONSE, useValue: res }
        ]
    }
});

Using this approach, you can separate URL into different parts and compute logic based on your needs. For example, if the API call URL is https://example.com/personal-information, then params[0] will be example.com and params[1] will be personal-information. This will help you solve the problem you are facing.