I
I
Igor Makhov2020-07-05 13:23:12
JavaScript
Igor Makhov, 2020-07-05 13:23:12

How to do multiple page processing?

Suppose I want to make a site on express.js, there will be several pages: home (/), news (/news/:articleid), contacts (/contacts), whatever else. What is the best way to organize this so that express always gives the same html file, and at the front js works like this:

const url = /* парсим url */
switch (url) {
    case '/':
        /* рендерим главную страницу */
    case '/news':
        /* ... */
}

or, so that the pages are generated on the back using, for example, ejs.
Also in the second case, the question arises as to how Vue / React can be used in the second case, because I heard that mixing template engines and similar frameworks is not worth it. Can I see an example of something similar somewhere (mb on Github)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Erik Mikoyan, 2020-07-06
@erik_mikoyan

You can just do something like

app.get('/|', function(request, response){
  console.log('/');
  response.sendFile(__dirname + "/build/index.html");
});

Then it will return the page to any request, and the routes will be at the front (You can replace
'/|' with any other regex you need).
As for the routes on the front, look at the documentation, it is quite good and simple there: https://learn-reactjs.ru/training-project/routing

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question