Marko: Express Configuration

Created on 9 Oct 2016  ·  5Comments  ·  Source: marko-js/marko

Hi guys!
This template engine is Awesome! Thanks for your hard work! :+1:

One question, exist any tuto to use with Express?

Sorry for this bad issue, Nicholls

Most helpful comment

The require('marko/node-require').install(); line just needs to run once at the very beginning. In your case, you just need to add it to the top of your ./app.js file.

We tried to make this clear in the docs for using Marko with Express: http://markojs.com/docs/marko/express/

It's also discussed in more detail as part of the Getting Started guide: http://markojs.com/docs/marko/get-started/#template-loading

If you still think there is room for improving the docs please let us know or submit a PR (at the top of every docs page on markojs.com there is a "Improve this page" page link).

All 5 comments

Guys check an issue please!

express

My Express route is:

var express = require('express');
var router = express.Router();
var template = require('../views/index.marko');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.marko(template, { title: 'Express' });
});

module.exports = router;

Hey @jdnichollsc, thanks for the feedback and welcome to Marko!

You are doing everything correctly, except you for got to register the Node.js require hook for Marko templates. You should add the require('marko/node-require').install(); line to your main JavaScript file:

require('marko/node-require').install(); // ← Add this line

var express = require('express');
var router = express.Router();
var template = require('../views/index.marko');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.marko(template, { title: 'Express' });
});

module.exports = router;

Without that line, Node.js will try to load Marko templates as JavaScript modules. The Node.js require hook for Marko registers a custom loader for Node.js that will automatically compile required .marko templates to JavaScript modules so that they can be loaded by the Node.js module loading system.

Hope that solves your problem, but let us know if you run into any other issues.

Cheers.

Hi Patrick, thanks for your quickly response my friend! 👍

Can I use your 'require('marko/node-require').install();' line in './app.js' or is required to use in every Express route? I'm using the Express generator.

Regards, Nicholls

What do you think to add your example into README file? Maybe it's very useful for others!

The require('marko/node-require').install(); line just needs to run once at the very beginning. In your case, you just need to add it to the top of your ./app.js file.

We tried to make this clear in the docs for using Marko with Express: http://markojs.com/docs/marko/express/

It's also discussed in more detail as part of the Getting Started guide: http://markojs.com/docs/marko/get-started/#template-loading

If you still think there is room for improving the docs please let us know or submit a PR (at the top of every docs page on markojs.com there is a "Improve this page" page link).

Was this page helpful?
0 / 5 - 0 ratings