Node.js - EJS 示例
我正在尝试使用嵌入式 Javascript 渲染器作为节点。我使用 npm 安装了它,如下所示: https://github.com/visionmedia/ejs
有以下代码,但它似乎不起作用:
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);
非常感谢任何帮助。
I am trying to use Embedded Javascript renderer for node. I installed it using npm, as given here: 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:(假设您安装了express和ejs模块)
并将视图放入'./views'中。将其命名为“index.ejs”并用一些 html 填充它:
对我有用!
try this: (assuming you have the express and ejs modules installed)
and put a view in './views'. call it 'index.ejs' and fill it with some html:
works for me!
您需要向响应发送一些内容。从 连接 hello-world
所以对于你您想要替换的应用程序:
类似这样的内容:
这有效吗?
You need to send something to the response. From the connect hello-world
So for your app you'll want to replace:
With something like:
Does that work?
将您的视图引擎设置为使用 ejs。
现在设置根路由,以便当您从浏览器访问服务器时它会加载某些内容,请参见下文。
Set your view engine to use ejs.
Now set up the root route so that it will load something when you access your server from a browser, see below.