Simplest possible node.js + nunjucks example
Start by initialising your Node application as follows:
cd njtestnpm init
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:
npm install express --save
Then create app.js
as follows:
var express = require( 'express' ), app = express(), nunjucks = require( 'nunjucks' ) ;// Define port to run server onvar port = process.env.PORT || 9000 ;// Configure Nunjucksvar _templates = process.env.NODE_PATH ? process.env.NODE_PATH + '/templates' : 'templates' ;nunjucks.configure( _templates, { autoescape: true, cache: false, express: app} ) ;// Set Nunjucks as rendering engine for pages with .html suffixapp.engine( 'html', nunjucks.render ) ;app.set( 'view engine', 'html' ) ;// Respond to all GET requests by rendering relevant page using Nunjucksapp.get( '/:page', function( req, res ) { res.render( req.params.page ) ;} ) ;// Start serverapp.listen( port ) ;console.log( 'Listening on port %s...', port ) ;
Now fire up a browser, go to http://localhost:9000 and up pops your page!
Hope that helps...
I created simple builder for Nunjucks + SCSS + TypeScript
DOCS: https://github.com/Artik-Man/SamuraiJS
NPM: https://www.npmjs.com/package/samuraijs
npm i samuraijs
Create samurai.js
file:
import {Samurai} from "samuraijs";new Samurai({ paths: { source: 'src', destination: 'dist' }});
Add the following lines to your package.json:
{ "scripts": { "serve": "node samurai.js --serve", "build": "node samurai.js --build" }, "type": "module"}
Place your *.njk files at the src/
directory, run
npm run serve
And open localhost:3000
Or npm run build
to build the project.
Dont think about build configuration. Just code your project!
I hope my builder solves your problem :)