I am using dustjs as the templating engine in an express app, and was wondering if anyone has successfully used the contextDump helper in a server side template in express? I followed the consolidate example for integrating dust with express, and it is working well. The helpers are there, as I'm able to do a simple test with the eq helper and it works. but when trying to dump the context I'm not seeing it anywhere. here is a sample template:
{>layout/}
{<content}
<ul>
{#users}
<li>{username} - Create New Task
<ul>
{#user.tasks}
<li>{title} - Delete Task |
Update Task</li>
{/user.tasks}
</ul>
</li>
{/users}
</ul>
{/content}
{#contextDump to="console"/}
Here's my app.js:
var express = require('express');
var app = express();
var http = require('http');
var path = require('path');
var db = require ('./models');
var dust = require('dustjs-linkedin');
var cons = require('consolidate');
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('models', db);
//configure dust
app.set('view engine', 'dust');
app.set('template_engine', 'dust');
app.engine('dust', cons.dust);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
//require routes
require('./routes')(app);
require('./routes/user')(app);
require('./routes/task')(app);
db
.sequelize
.sync({ force: true })
.complete(function (err) {
if (err) {
throw err;
} else {
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
}
});
and here's package.json:
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.4.6",
"sequelize": "~2.0.0-beta.5",
"mysql": "~2.0.0-rc2",
"lodash": "~2.4.1",
"async": "~0.2.9",
"dustjs-linkedin": "~2.2.2",
"dustjs-helpers": "~1.1.1",
"consolidate": "~0.10.0"
}
}
Thanks!
Related
I'm currently trying to learn MEAN stack. The tutorial are about a task manager. Right now I'm trying to connect to mongodb atlas to retrieve sample data in database but they are not showing up http://localhost:3000/api/tasks
Here is my tasks.js file
var express = require('express');
var router = express.Router();
var mongojs = require('mongojs');
var db = mongojs('drivers',['tasks']);
router.get('/tasks', function(req, res, next){
db.tasks.find(function(err, tasks){
if(err){
res.send(err);
}
res.json(tasks);
});
});
module.exports = router;
Here is my server.js file
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var task = require('./routes/tasks');
var app = express();
var port = 3000;
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
app.use(express.static(path.join(__dirname, 'client')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/', index);
app.use('/api', task);
app.listen(port, function(){
console.log('Server started on port' +port);
});
Here is my package.json file
{
"name": "mytasklist",
"version": "1.0.0",
"description": "Task Manager",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"ejs": "^2.6.1",
"express": "^4.17.1",
"mongojs": "^2.6.0"
}
}
Problem solved, I pasted the wrong version for my nodejs driver.
need to use mongojs version 3.0.0 to connect to atlas
This app works fine locally and works on openshift with no errors, however the views aren't rendering it is just printing the source code. I tried deleting and recreating the app but still no luck.
Here is part of my server.js file:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var fs = require("fs");
var handlebars = require("handlebars");
var WebPageTest = require('webpagetest');
// Web Page Test - API key.
var wpt = new WebPageTest('www.webpagetest.org', '1234567890');
var router = express.Router();
app.use("/css", express.static(__dirname + '/css'));
app.use("/js", express.static(__dirname + '/js'));
app.use("/img", express.static(__dirname + '/img'));
/***** Views *****/
// Homepage.
app.get('/', function(req, res) {
var template = fs.readFileSync("views/index.html", "utf8");
// handlebars data, optional.
var source = {
message : "Homepage!"
};
var pageBuilder = handlebars.compile(template);
var pageText = pageBuilder(source);
res.writeHead(200, {"Context-Type": "text/html"});
res.write(pageText);
res.end();
});
/***** Start App *****/
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 5000;
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
app.listen(server_port, server_ip_address, function(){
console.log("Listening on " + server_ip_address + ", server_port " + server_port);
});
Here is part of the package.json
{
"name": "wpt",
"version": "1.0.0",
"description": "",
"main": "server.js",
"dependencies": {
"body-parser": "^1.10.0",
"express": "^4.10.6",
"webpagetest": "^0.3.1",
"mongoose": "^3.8.21",
"handlebars": "^2.0.0",
"express3-handlebars": "^0.5.2",
"fs":"*"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
I added fs to the dependencies manually but it didn't seem to have any effect.
It's probably a lot easier to just use a handlebars Express views engine such as hbs. Example:
// ...
var hbs = require('hbs');
app.engine('hbs', hbs.__express);
app.engine('html', hbs.__express);
app.set('view engine', 'hbs');
app.set('views', __dirname + '/views');
hbs.localsAsTemplateData(app);
app.get('/', function(req, res) {
res.render('index.html', {
message: 'Homepage!'
});
});
// ...
Iam getting following error if I try to open the localhost:3000 or localhost:3000/login. Can someone help me what is the problem? It is very strange. Many thanks
My Code
var express = require('express');
var crypto = require('crypto');
var bodyParser = require('body-parser');
var flash = require('connect-flash');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var path = require ('path');
var port = process.env.PORT || 3000;
var MongoClient = require('mongodb').MongoClient,
ObjectID = require('mongodb').ObjectID,
url = require('url');
var db;
//var mongo;
//var collection;
//dataExt = require('./routes/serverExtend');
// setup middleware
var app = express();
app.use(bodyParser());
app.use(flash());
app.use(cookieParser('secret'));
app.use(session({cookie: { secret: 'keyboard cat', maxAge: 60000 }}));
app.use(express.static(__dirname + 'public')); //setup static public directory
app.set('views', __dirname + 'testapp/views'); //optional since express defaults to CWD/views
app.set('view engine', 'ejs');
// Start server
app.listen(port);
console.log('App started on port ' + port);
// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/testDB", function(err, database) { //"mongodb://localhost:27017/test"
if(err) throw err;
db = database;
});
app.get('/', function(req, res) {
res.render('index.ejs', { message: req.flash('signupMessage')}); // load the index.ejs file
app.get('/login', function(req, res) {
res.render('login.ejs');
});
Here is my package.json
{
"name": "NodejsStarterApp",
"version": "0.0.1",
"description": "A sample nodejs"
"dependencies": {
"express" : "~4.0.0",
"ejs" : "*",
"mongodb":"*",
"connect-flash" : "~0.1.1",
"morgan": "~1.0.0",
"body-parser": "*",
"cookie-parser": "~1.0.0",
"express-session": "~1.0.0"
},
"engines": {
"node": "0.10.26"
},
"repository": {}
}
Here is the problem.
change this line
app.set('views', __dirname + 'testapp/views');
to
app.set('views', __dirname + 'views');
Because when you say just __dirname + 'views' it looks in {appName}/views/fileToRender. But because you are saying __dirname + 'testapp/views' it will search {appName}/testapp/views/fileToRender.
I am trying to get dustjs-linkedin working with an express 3 project, however I can't seem to get past this error:
Error: Template name parameter cannot be undefined when calling dust.compile
at Object.compiler.compile (/home/user/project/node_modules/dustjs-linkedin/lib/compiler.js:21:16)
at Object.dust.compileFn (/home/user/project/node_modules/dustjs-linkedin/lib/dust.js:109:37)
at Function.exports.dust.render (/home/user/project/node_modules/consolidate/lib/consolidate.js:226:56)
at /home/user/project/node_modules/consolidate/lib/consolidate.js:146:25
at /home/user/project/node_modules/consolidate/lib/consolidate.js:99:5
at fs.js:266:14
at Object.oncomplete (fs.js:107:15)
I can reproduce this with a brand new express 3 project like so:
app.js
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var app = express();
// added these 3
var dust = require('dustjs-linkedin');
var cons = require('consolidate');
app.engine('dust', cons.dust);
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
// added this 1
app.set('view engine', 'dust');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
package.js
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.4.7",
"dustjs-linkedin": "*",
"consolidate": "*"
}
}
routes/index.js
exports.index = function(req, res){
res.render('index', { title: 'Express' });
views/index.dust
Title: {title}
The strange thing is, I have a project using express 3 and dustjs-linkedin that is working just fine. I ran a diff on the files in the dustjs-linkedin module and the working version has a lot of files in dist while the project throwing the error only has a few, namely:
working/node_modules/dustjs-linkedin/dist:
dust-core.js dust-core.min.js dust-full.js dust-full.min.js LICENSE
workingProject/node_modules/dustjs-linkedin/dist:
dust-core-1.1.1.js dust-core-2.0.0.min.js dust-full-1.2.0.js dust-full-2.0.1.js
dust-core-1.2.0.js dust-core-2.0.1.js dust-full-1.2.1.js dust-full-2.0.2.js
dust-core-1.2.1.js dust-core-2.0.2.js dust-full-1.2.2.js dust-full-2.0.3.js
dust-core-1.2.2.js dust-core-2.0.3.js dust-full-1.2.2.min.js dust-full-2.1.0.js
dust-core-1.2.2.min.js dust-core-2.1.0.js dust-full-1.2.3.js dust-full-2.2.0.js
dust-core-1.2.3.js dust-core-2.2.0.js dust-full-1.2.3.min.js dust-full-2.2.2.js
dust-core-1.2.3.min.js dust-core-2.2.2.js dust-full-1.2.4.js dust-full-2.2.2.min.js
dust-core-1.2.4.js dust-core-2.2.2.min.js dust-full-1.2.5.js dust-full-2.2.3.js
dust-core-1.2.5.js dust-core-2.2.3.js dust-full-1.2.6.js dust-full-2.2.3.min.js
dust-core-1.2.6.js dust-core-2.2.3.min.js dust-full-2.0.0.js
dust-core-2.0.0.js dust-full-1.1.1.js dust-full-2.0.0.min.js
This demo of a 'working' project gives me the same error:
https://github.com/chovy/express-template-demo
[Edit]
An issue is discussed here:
https://github.com/linkedin/dustjs/commit/e5ebff0f7b32f8ff0883be7f7924507b314eef1d
In [/node_modules/]consolidate/lib/consolidate.js go to exports.dust.render function:
...
try {
var tmpl = cache(options) || cache(options, engine.compileFn(str));
tmpl(options, fn);
} catch (err) {
fn(err);
}
...
engine.compileFn is called sending str as parameter, str is actually the template itself:
Title: {title}
engine.compileFn is compiler.compile(source, name) in [/node_modules/]dustjs-linkedin/lib/compiler.js.
source is template sent, however name which is the template name and should be index in our case, is never set (undefined).
Alternatively, I prefer to use Payapl's Adaro library for express 3.x and linkedin-dustjs. It's part of their project kraken.js, but can be used as a standalone dust wrapper for express:
https://github.com/paypal/adaro
As mentioned in README.md:
var express = require('express');
var dustjs = require('adaro');
var app = express();
app.engine('dust', dustjs.dust({});
app.set('view engine', 'dust');
// For rendering precompiled templates:
// app.engine('js', dustjs.js({ ... ));
// app.set('view engine', 'js');
I'm using node version 0.10.3 and Express 3.1.1.
I'm trying to return a simple response, but I'm getting the following error:
http.js:692
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:692:11)
I've read a lot about it, and tried many solutions but I'm still getting that error.
I've used node-inspector and i'm getting the error after that line -
res.render('index', { title: 'Express' });
app.js:
/**
* Module dependencies.
*/
var express = require('express')
, http = require('http')
, path = require('path')
, socketio = require('socket.io');
var app = express()
, server = http.createServer(app)
, io = socketio.listen(server);
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'hjs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
server.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
io.configure('production', function(){
io.enable('browser client etag');
io.set('log level', 1);
});
io.configure('development', function(){
io.set('log level', 1);
});
io.sockets.on('connection', function(socket) {
socket.on('event', function(event) {
socket.join(event);
});
});
require('./routes')(app, io);
routes/index.js:
var utils = require('../utils')
, config = require('../config')
, io;
module.exports = function(app, socketio) {
io = socketio;
app.get('/', index);
};
var index = function(req, res){
res.render('index', { title: 'Express' });
};
views/index.hjs:
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>{{ title }}</h1>
<p>Welcome to {{ title }}</p>
</body>
</html>
package.json:
{
"name": "blabla",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "app.js"
},
"dependencies": {
"express": "3.0.3",
"hjs": "0.0.4",
"cradle": "0.6.4",
"twiliosig": "0.0.1",
"socket.io": "0.9.11"
},
"subdomain": "blabla",
"engines": {
"node": "0.6.x"
}
}
Any Clue?
Seems to be an error with your version of socket.io.
Use this:
"dependencies": {
"express": "3.0.x",
"hjs": "0.0.4",
"cradle": "0.6.4",
"twiliosig": "0.0.1",
"socket.io": "0.9.x"
}
Tested with socket.io#0.9.16