You're viewing old version number 4. - Current version 1 min Node.js - Express - Handlebars - Ex 2 #nodejs - #javascript - #programming // app2.js var express = require('express'); var exphbs = require('express-handlebars'); var simple = require('./simple'); var app = express(); app.engine('handlebars', exphbs({defaultLayout: 'main'})); app.set('view engine', 'handlebars'); app.get('/', simple.home); app.get('/simple', simple.gorilla); app.listen(3000); /////////////////////////////////////////////// // simple.js var PageGlobals = require('./pageglobals'); var globals = new PageGlobals(); var simpleControllers = { 'home': function (req, res) { var data = {default_values: globals.getvalues()}; res.render('home', data); }, 'gorilla': function (req, res) { var data = {name: 'Gorilla', default_values: globals.getvalues()}; res.render('simple', data); } }; module.exports = simpleControllers; /////////////////////////////////////////////// pageglobals.js require('./modules/date.format'); var myDate = new Date(); var dt_str = myDate.format('D, M j, Y - g:i a') + " UTC"; // constructor var PageGlobals = function() { }; PageGlobals.prototype.getvalues = function() { var page_values = { datetime: dt_str, title: 'nodejs testing', app: 'app2.js' }; return page_values; }; module.exports = PageGlobals; /////////////////////////////////////////////// // date.format.js created by: // http://github.com/jacwright/date.format /////////////////////////////////////////////// // /views/ directory: // home.handlebars **Include template "> header" not found.** <h1>home.handlebars</h1> **Include template "> footer" not found.** // simple.handlebars **Include template "> header" not found.** <h1>simple.handlebars</h1> <h2>name = **Include template "name" not found.**</h2> **Include template "> footer" not found.** // /views/partials/ directory: // header.handlebars <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>**Include template "default_values.app" not found.** - **Include template "default_values.title" not found.**</title> </head> <body> // footer.handlebars <p> **Include template "default_values.datetime" not found.** </p> </body> </html> // /views/layouts/ directory: // main.handlebars **Include template "{body" not found.**} /////////////////////////////////////////////// $ curl http://localhost:3000 $ curl http://localhost:3000/simple From JR's : articles 197 words - 2131 chars - 1 min read created on Nov 06, 2014 at 04:44:28 pm updated on Nov 06, 2014 at 04:58:32 pm - # source - versions