I'm using ExpressJS with normal HTML templates (no Jade or similar template engines). Since I'm about to add MongoDB to my App, I need to inject data in to my HTML files. I know that it's no problem when I use Jade or similar template engines. Is it possible to directly inject it to my HTML files without using Jade? If not, is it possible to use Jade and still having my HTML files?
Here is my app.js:
var express = require('express');
var server = express.createServer();
server.configure(function () {
server.use('/bootstrap', express.static(__dirname + '/bootstrap'));
server.use('/css', express.static(__dirname + '/css'));
server.use('/js', express.static(__dirname + '/js'));
server.use(express.static(__dirname + '/html'));
});
server.set('views', __dirname + '/html');
server.register('.html', require('handlebars'));
server.set('view options', { layout: false });
server.use(express.bodyParser());
server.use(express.cookieParser({ secret: "keyboard cat" }));
var memStore = require('connect').session.MemoryStore;
server.use(express.session({ secret: "keyboard cat", store: memStore( {
reapInterval: 60000 * 10
})}));
server.listen(1337);
console.log('Express server started on port %s', server.address().port);
/** SESSIONS **/
function requiresLogin(req, res, next) {
if (req.session.user) {
next();
}
else {
res.redirect('/login'); // TODO: process redirect
}
}
var pseudoUsers = require('./users');
server.get('/login', function(req, res) {
res.render('login.html');
});
server.post('/authenticated', function(req, res) {
pseudoUsers.authenticate(req.body['emailInput'], req.body['passwordInput'], function(user) {
if (user) {
req.session.user = user;
res.redirect('/dashboard');
}
else {
res.redirect('/login?wrongCredentials=true');
}
});
});
/* ROUTERS ****************/
/**************************/
server.get('/home', function(req, res) {
res.render('index.html');
});
[some more routes..]
On the assumption that you need to inject dynamic data (data that changes over time and has other dependencies, like usernames etc), you will be using templates one way or the other. If you are not willing to use any pre-made template engine, most likely you will end up creating your own which will most likely be inferior to most of the other engines that have some billion pageviews behind them.
You specifically mention that you do not want to use Jade. But your code does pull in handlebars which is another template engine (http://handlebarsjs.com/), which basically allow you to mix variables with your html templates using {{ somevarref }}
.
You could also avoid templates all together and create all your html dynamically with code, but that's usually done behind the scenes for you with any template engine, so why go through all that trouble manually?
Finally, you could also do generate your html client side, possibly using DOM manipulation to get all the right pieces in place, but based on your question I'm guessing either that does not fit your use case, and/or that's probably a bit beyond what is currently needed.