【发布时间】:2012-02-05 22:38:27
【问题描述】:
您好,我正在使用 Node.js 与 Fulephp 进行一些测试。我有一个简单的设置,我试图看看什么更快。我在 mogodb 中有 1 万条记录被拉入视图。设置很简单,没有 js,没有 css,只有最少的 html。我很快注意到 php 设置的速度是原来的两倍。起初,我认为 nodejs 速度较慢,然后继续我的生活。然而,我决定尝试使用我用作模板引擎的不带翡翠的节点,幸运的是,我在 stackoverflow 上发现了一篇关于翡翠背后的哲学不是速度而是优雅。然后我决定尝试没有任何临时节点的节点。引擎。但是我很快就遇到了一个问题,因为我意识到我不知道如何将数据从数据库和节点传递到客户端。我在恐惧和绝望中度过了一个漫长的夜晚。在某个时候,我得出一个结论,即我需要 socket.io 的帮助。虽然我最终能够连接到 socket.io,但我仍然无法弄清楚如何传递数据。然后我决定回到使用温度计。引擎,但这次我决定尝试ejs。最终,我能够呈现一些具有以下形式的数据 [object Object],但它不是 10000 条记录,更像是 25 条。我决定做正确的事情并在这里发布我的问题。我想在没有模板引擎的情况下渲染视图,看看我的假设是否正确。 毕竟我正在尝试做两件事,了解如何将数据传递到客户端表单 node.js 并查看它是否会提高我的性能。
这是我的 app.js 和一些 cmets:
/**
* Mongo DB
*/
var mongous = require('mongous').Mongous,
dbCollection = 'test.personnel';
/**
* Module dependencies.
*/
var express = require('express'),
app = module.exports = express.createServer(),
pub = __dirname + '/public';
// Configuration
app.configure(function(){
app.set('view options', {layout: false});
//not sure if i need these here, but left it in case
app.use(app.router);
app.use(express.static(pub));
});
//Simple templating
//I took this example from stackoverflow,
//can't find that post anymore,
//though I can look if need be
app.register('.html', {
compile: function(str, options){
return function(locals){
return str;
}
}
});
// Routes
//This is where data is right now, it need to end up on the client side
//This was working with jade and kinda worked with ejs (though I am not sure because I was getting [object Object])
//--------------------------------------------------
app.get('/', function(req, res){
mongous(dbCollection).find(function(output){
res.render('index.html',{
//the data is here, it works, i tested it with console.log
data: output.documents
});
});
});
//--------------------------------------------------
app.configure('production', function(){
app.use(express.errorHandler());
});
app.listen(3000);
console.log('Express server listening on port %d in %s mode', app.address().port, app.settings.env);
这是我的观点:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Index</title>
</head>
<body>
<div id="data">
Data needs to end up here. Somehow...
</div>
</body>
</html>
正如你所见,那里并不多。现在我知道我很可能需要在客户端使用某种模板引擎,一旦我在客户端获得数据,我就可以自己解决。最后可能会更慢,但我的主要目标是了解如何将 node.js 中的数据传递给客户端,以便我可以继续实验。如果可以,请提供帮助,这将提高我的理解节点显着。谢谢。
编辑: 在你们所有人的帮助下,尤其是 josh3736,这就是我最终得到的结果,如果你有兴趣的话...... http://pastie.org/private/z3fjjbjff8284pr2mafw
【问题讨论】:
标签: javascript node.js