mlibre
mlibre

Reputation: 2540

write nodejs web application like php

How can I write nodejs (with and without express framework) app that do such think in php:

<doctype hyml>
<html>
<body>
<h1> <?php echo "Hi"; ?> </h1>
</body>
</html>

with or without express.js.

Upvotes: 0

Views: 296

Answers (1)

Jonas Vinther
Jonas Vinther

Reputation: 271

You will have to setup your node.js project to use a template engine like jade.

app.js

By doing that you need to install jade with npm and require the dependency into your project.

var express = require('express');
var jade = require('jade');
var app = express();

Then you have to tell express.js to use the jade template engine.

app.set('view engine', 'jade');

Then you use the render function to render the index.jade with the second param as an object.

app.get('/', function(req, res) {
    res.render('index', {
        msg: 'Hi'
    });
}

views/index.jade

The #{msg} placeholder will then be compiled into 'Hi'.

html
    body
        h1 #{msg}

Upvotes: 2

Related Questions