Does jadeify download templates 1 by 1 on demand? - node.js

I am thinking of using Jade templates on client side (with BackboneJS). Seems like I can use jadeify. But was wondering, how does it work? Does it download each template 1 by 1 or compile all templates on serverside then serve a "compiled" templates file to the client? Looking at the source, it looks surprisingly short:
https://github.com/substack/node-jadeify/blob/master/jadeify.js
var views = require('jadeify/views'); ...
module.exports = function (file, vars, opts) {
...
return $(jade.render(views[file], opts));
};
Line 1: can I even require a folder like that? Can I say views is now an array of scripts under that directory? That appear to be the case from Line 5, where views[file] is used.
But it calls jade.render. Won't it be more efficient to call jade.compile and cache it first? Or is it already cached somewhere?

Related

How to access routes folder in sailsjs

My route.js file is becoming larger. Is there any way to split the file based on controller?
I want to create separate route file for each folder and merge it.
Basically you can do whatever you want in your project. One solution is to use the route.js file as a compilation file of your route configurations. You can write a function and execute it in place like that:
var routes = {};
var assemble = function() {
//do assembling stuff here and add your routes to the routs object
}
assemble();
module.exports.routes = routes;
This should be enough. In your assemble function you can use some smart way to find configurations so that your code is universal and from now on whatever config you write it would get merged automatically.

npm concat a static file to all files in directory using gulp

I have tried using npm-concat but it concats all files from folder. but my requirement is something like this-
I have a single file called "header.html","footer.html" and i have folder called view in which i have raw text of html.
tpl
---- header.html
---- footer.html
view
---- view1.html
---- view2.html
Now i want to prepend header.html to all files under view and I want to append footer.html to all files under view folder. How we can achieve this ?
I think you would do better to consider using a template engine such as handlebars to accomplish this task, rather than concatenating files. But I'll try to answer your question since you asked.
I don't know of any plugin that can do this in an optimal way. However, I have written a plugin called gulp-transform that will let you do this fairly easily.
const gulp = require('gulp');
const transform = require('gulp-transform');
const readFileSync = require('fs').readFileSync;
const HEADER = readFileSync('tpl/header.html');
const FOOTER = readFileSync('tpl/footer.html');
gulp.task('html', () => {
return gulp.src('view/*.html')
.pipe(transform(contents => Buffer.concat([HEADER, contents, FOOTER])))
.pipe(gulp.dest('dist'));
});
This is a bit ugly, and in fact I would only recommend using this plugin for a quick fix when there is no better option. A better solution would be to define a custom plugin that does exactly this. If you think it would be helpful, I can show you how you might do that. This is a bit more involved though. I really think your best option is to use a template engine.

node.js/Jade - How to pre-compile jade files and cache it?

Framework: node.js / express.js / Jade
Question: in production env, when a jade file is rendered by express, jade cache's it so future renders are faster.
When I start node.js app, how can I pre-compile (or) pre-render (like warmup) all the jade files so its already in cache when requests start to come in...
I can use a folder recursion, I just need to know how to pre-compile (or) pre-render.
Is this possible?
Jade has template pre-compiling and caching built in.
http://jade-lang.com/api/
Simply specify cache: true option to jade.compileFile, and iterate through all of your template files.
var options = {cache: true};
// iterate/recurse over your jade template files and compile them
jade.compileFile('./templates/foo.jade', options);
// Jade will load the compiled templates from cache (the file path is the key)
jade.renderFile('./templates/foo.jade');
If you're not using any parameters, you can compile jade templates directly to HTML with grunt or gulp and make it watch for file modifications
Try it from the command-line:
jade view/map-beacons.jade -D
If you do need to use parameters I would use something like in Andrew Lavers answer.
compileFile returns a function which you can use to pass in parameters i.e. fn({ myJsVar: 'someValue' })
There is also a client option in the command-line but I didn't find any use for it :
jade view/map-beacons.jade -cD
i do this solution, this code outside http.createServer function
let cache_index=jade.renderFile('index.jade');
and when return view
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end(cache_index);
when use this solution server return index to 1ms
but without solution server return index between 150ms to 400ms
result:
Picture 1 with cache
Picture 2 without cache

extending controllers with raw js files in sailjs

I'm using "sails": "~0.10.5" and I'm making a controller / webservice that takes get and post. Everything's fine, but I want to bring in a couple of js files I'd written earlier for processing uploaded csv's. I created a src folder on the root of my sails.js project that require a few modules, etc. I'm wondering is this the "sails.js way" of adding util / helper files?
Should I go through the extra steps of making them an npm module or is that overkill?
Yes there is :
Those file/library have to be added in api/services/ folder and be "ES6 modules"
for example create a file api/services/Hello.js
// say hello
module.exports = {
hello: function(name){
sails.log.warn("Hello was called !"); //not a good log ...
return "hello" + name + sails.config.local.helloconf; //you can then access configs and some other sails native functions;
}
};
in your controller you can then simply do something like Hello.hello("bob");

Including a .js file in node.js

I am trying to include this script in my app: http://www.movable-type.co.uk/scripts/latlong.html
I have saved it in a file called lib/latlon.js, and I am trying to include it like this:
require('./lib/latlon.js');
How should I go about including a JS library like this?
First of all, you should take a look at the Modules documentation for node.js:
http://nodejs.org/docs/v0.5.5/api/modules.html
The script you're trying to include is not a node.js module, so you should make a few changes to it. As there is no shared global scope between the modules in node.js you need to add all the methods you want to access to the exports object. If you add this line to your latlon.js file:
exports.LatLon = LatLon;
...you should be able to access the LatLon function like this:
var LatLonModule = require('./lib/latlon.js');
var latlongObj = new LatLonModule.LatLon(lat, lon, rad);

Resources