Node.js - EJS 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4600952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:48:05  来源:igfitidea点击:

Node.js - EJS example

node.jsejs

提问by jeffreyveon

I am trying to use Embedded Javascript renderer for node. I installed it using npm, as given here: https://github.com/visionmedia/ejs

我正在尝试为节点使用嵌入式 Javascript 渲染器。我使用 npm 安装它,如下所示:https: //github.com/visionmedia/ejs

And I have the following code, but it does not seem to work:

我有以下代码,但它似乎不起作用:

var connect = require('connect'),
 ejs = require('ejs');

var server = connect.createServer(
    connect.bodyDecoder(),
    connect.methodOverride(),
    connect.staticProvider(__dirname + '/public'),
    function(req,res) {
     ejs.render('hi');
    }
);


server.listen(9000);

Any help greatly appreciated.

非常感谢任何帮助。

采纳答案by RandomEtc

You need to send something to the response. From the connect hello-world

您需要向响应发送一些内容。从连接你好世界

var connect = require('../../lib/connect');

var server = connect.createServer(function(req, res){
  var body = 'Hello World';
  res.writeHead(200, {
      'Content-Type': 'text/plain'
    , 'Content-Length': body.length
  });
  res.end(body);
});

server.listen(3000);
console.log('Connect server listening on port 3000');

So for your app you'll want to replace:

因此,对于您的应用程序,您需要替换:

function(req,res) {
 ejs.render('hi');
}

With something like:

像这样:

function(req,res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end(ejs.render('hi'));
}

Does that work?

那样有用吗?

回答by Elmer

try this: (assuming you have the express and ejs modules installed)

试试这个:(假设你安装了 express 和 ejs 模块)

var express = require('express');
var app = express.createServer();
app.configure(function() {
    app.use(express.bodyParser());
    app.use(express.static('./static/'));
    app.use(app.router);
});
app.set('view engine', 'ejs');
app.set('view options', {
    layout: false
});
app.get('/', function(req, res) {
res.render('index', {
    message : 'De groeten'
});
});
app.listen(3000);

and put a view in './views'. call it 'index.ejs' and fill it with some html:

并在“./views”中放置一个视图。将其命名为“index.ejs”并用一些 html 填充它:

<html>
<head>
<title></title>
</head>

<body>
<p>
<%= message %>
</p>
</body>
</html>

works for me!

对我来说有效!

回答by Joey Nelson

Set your view engine to use ejs.

将您的视图引擎设置为使用 ejs。

app.set("view engine", "ejs");

Now set up the root route so that it will load something when you access your server from a browser, see below.

现在设置根路由,以便在您从浏览器访问服务器时加载一些内容,见下文。

var app = express();

// ROOT ROUTE
app.get("/", function(req, res) {
  res.render("landingpage"); // use to render an ejs template page
  res.send("hello world"); // to render an empty page with the hello world message
});