Answer the question
In order to leave comments, you need to log in
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 -->
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);
});
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question