Says we have a personal blog page, once the user visit '/home', the server returns all the blog posts he has written, meanwhile I also want to get data from the server so I can handle the data with template in front-end. In Express, is there any way to perform action like this:
app.get('/home', function () {
Post
.getAll()
.then(function (posts) {
res.send(posts)
})
res.render('home')
})
The reason I want to do this is to minimal ports and also gathering them up by functionalities, or have I define data-ports for each one?
Thanks 4 help
No, you can't do that. Separate your data and template providing logic like this sample:
app.get('/home', function () {
res.render('home');
});
app.get('/api/posts', function () {
Post
.getAll()
.then(function (posts) {
res.send(posts);
});
});