编写nodejs web应用程序,如php


write nodejs web application like php

如何在php中编写nodejs(有或没有express framework)应用程序:

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

你必须设置你的node.js项目来使用像jade这样的模板引擎。

app.js

这样做,你需要使用npm安装jade,并将依赖项要求到你的项目中。

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

然后你必须告诉express.js使用jade模板引擎。

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

然后使用render函数来渲染索引。使用第二个参数作为对象。

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

视图/index.jade

#{msg}占位符将被编译成'Hi'。

html
    body
        h1 #{msg}