D
D
dmitriu2562020-01-10 00:59:05
Node.js
dmitriu256, 2020-01-10 00:59:05

How to make a dynamic template in expressjs?

The question has been ripe for a very long time, because the question always remains what's next ?, and always at the same stage
And so the essence
What I want to get:
- each section is independent and receives data from the database along a separate route
- each section has its own data and connections between tables
(/features, /clients, /products...) How to correctly create a dynamic site template (for example, I took a landing page) specifically for
nodejs-express
). I cut the static template (in sections), one section is separate for the template (static), then I link everything together
in index.ejs .

<%- include('layout/head'); -%>

<%- include('navigation'); -%>
<!-- /.navigation -->

<%- include('header'); -%>
<!-- END HEADER -->

<%- include('main'); -%>
<!-- /#main.main -->
<%- include('features'); -%>
<!-- /#features.features -->

How to make each section dynamic, I take data from mysql (each section has its own table with data, etc.).
How to implement it correctly?
1) For each section, create your own GET request and make a request to the server via ajax? as for me not a very reasonable solution, especially if there are many sections.
2) Create a single query to the database on the root route - if there is very little data, then it will do, but a very bad solution.
3) Create separate GET requests (for each section) and use the Angular framework to refer to these routes in JSON format, for example?
const connection = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'bd_cardboard'
});


app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(express.static(__dirname + '/public'));

/*Routing*/

app.get('/', function(req,res){
    res.render('index');
});

app.get('/features', function(req,res){
    connection.query('SELECT features.title, features.descr, features.photo_id, photos.path, photos.category, photos.exp, photos.altText FROM features\n' +
        'INNER JOIN photos WHERE features.photo_id = photos.id;', function(err, rows){
        if(err) throw new  Error;
        res.json(rows);
    });
});

I will be grateful for your help! I really want to get to the bottom of this!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question