Probably will never use node.js nor Nunjucks for any real development, but now for some reason need:
- precompile the some simple templates to javascript with
Nunjucks - run the precompiled templates under the
node.js
I have done:
- installed
node.jsand thenpm(e.g. havenodeand thenpmcommand) mkdir njtest && cd njtest- installed the nunjucks with the
npm install nunjucks(got anode_modules/nunjucksdirectory) mkdir templatesin the templates i have created two files
index.htmlandlayout.htmlwith the followingjinja2/nunjuckscontentlayout.html
<!doctype html>
<head>
<title>simple example</title>
</head>
<body>
<h1>Simple example</h1>
{% block body %}{% endblock %}
</body>
index.html
{% extends "layout.html" %}
{% block body %}
hello world
{% endblock %}
- I precomplied the templates with the
./node_modules/nunjucks/bin/precompile templates >templates.js
and in the templates.js I have the precompiled code.
What I should to do next to get an running web-server what will use the precompiled template.js?
Please, don't search anything advanced behing this question. This is probably an stupid-simple question for someone who know node and javascript.
What i know, will need, create a file let says app.js and need run it with the node - but what should contain?
require 'nunjucks';
and probably something like: var res = nunjucks.render('templates.js'); and what else? (the simplest possible (one time) solution). Note: want use Nunjucks server-side and not in the browser.
Start by initialising your Node application as follows:
You can hit "Enter" to accept the defaults for most of the questions, if you're doing this after creating app.js then it'll automatically detect this and use it as the entry point for your simple server.
Install Express:
Then create
app.jsas follows:Now fire up a browser, go to http://localhost:9000 and up pops your page!
Hope that helps...