I find it difficult to understand exactly how node.js serves dynamic content. So, let's say we have the following code that displays the home page:
var express = require('express'), app = express(); app.get('/', function(req,res){ res.render('home.html'); });
However, suppose this home page should be a user profile in which you retrieve user information from the database, which leads to the code:
var express = require('express'), mongoose = require('mongoose'), app = express(); mongoose.connect('mongodb://localhost/ExampleDB'); app.get('/:id', function(req,res){ User.findOne({_id: req.id}, function (err, user){ var name = user.name; var profilePic_uri = user.profilePic_uri; res.render('home.html'); });
So, ideally, home.html is just a template page in which you can set the profile picture of the user, his name, etc. in the route handler. That's right, because the idea of โโnode is that this app.js should be able to handle pulling dynamic content from the database at runtime. Where I have problems, you understand exactly how the rendering of dynamic pages works with node. The html page is a static page. You can't really render a php or asp page because, well, that doesn't really make sense?
What leaves me wondering how this is done?
cg14
source share