包含 Node.js 和 mongodb 的整个网站?
我已经使用 PHP 和 mySQL 作为后端构建了多个网站,并且相信我对两者都相当熟悉。然而,在研究我的新网站时,我遇到了 node.js 和 mongodb(以及 socket.io,因为该网站将包含聊天功能)。
我决定使用 node.js 和 mongodb 来运行聊天 - 但不知道我是否应该用这两件事来完成整个网站? 既然我无论如何都要运行一台节点服务器,我是否应该运行另一个(单独的)节点服务器来托管网站?或者这是一个坏主意? - 稳定吗? 我可以用 PHP 进行编程,并且仍然使用 mongodb - 但节点不是更快吗?
还有一个问题: 我计划使用 ajax 来处理页面上的所有帖子 - 但由于我已经准备好使用 socket.io 进行聊天 - 我应该使用它来完成所有帖子请求吗? 对于 ajax,我计划使用 jQuery(也用于所有前端效果)。
I've build several websites using PHP and mySQL as backend, and believe that I'm fairly familiar with both. However during research for my new website I've come across node.js and mongodb (and socket.io, since the site is gonna contain a chat).
I've decided to use node.js and mongodb to run the chat - but don't know if I should just do the entire site with those two things?
Since I'm gonna run a node server anyway should I just run another (seperate) one hosting the website? Or is that an bad idea? - is it stable?
I could do the programming in PHP and still be using mongodb - but wouldn't node be way faster?
And another question:
I've planned to use ajax to handle all the posts to the page - but since I'm allready using socket.io to the chat - should I do all my post request using that?
For the ajax I've planned to use jQuery (also for all frontend effects).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你想学习 Node.js,那么没有什么比编码更好的了。
您可以使用现有服务器并在其他空闲端口(o)上运行您的node.js应用程序。我认为对于学习节点你不需要有专用的机器。
即使 Node.js 的版本都是稳定版本,但在功能冻结的 1.0 版本出现之前,其 API 可能会发生重大变化。
它很可能(并且肯定)会。
我建议坚持 MVC 模型并使用 Express,因为如果您使用 socket.io 来处理经典的东西,您可能会遇到很多耗时的麻烦。 Socket.io 主要用于实时功能以及与之相关的功能。
If you want to learn node.js then there is nothing better than coding it.
You can use existing server and run your node.js app on other free port(o). I think for learning node you don't need to have dedicated machine.
Even versions of node.js are stable releases, however until there is 1.0 with feature freeze there could be breaking changes to its API.
It most probably (and definitely) would.
I would recommend stick to MVC model and use express since you can get into lot of time consuming troubles if you would use socket.io for classic stuff. Socket.io is namely for real-time functionality and things related to that.
已经有一些可靠的 Node.js Web 框架,特别是 Express。这是一篇非常好的文章,概述了构建 Node.js 网站的一些教训和经验:
在 Node.js 中构建一个真正的网站感觉如何
关于你的第二个问题,最好还是使用 AJAX 处理程序和 HTTP 与 jQuery。我不确定 jQuery 是否支持通过原始 TCP 套接字进行回调。
There are already some solid web frameworks for node.js, in particular check out Express. Here's a really good article outlining some lessons and experiences from building a node.js website:
What it’s like building a real website in Node.js
Regarding your second question, it's probably still best to use AJAX handlers and HTTP with jQuery. I'm not sure that jQuery supports callbacks over raw TCP sockets.
node.js + express + jade + stylus + jQuery 是我的首选环境。
使用 forever 自动重新启动服务器 即使我有错误,我也从未遇到过任何真正的正常运行时间问题定期使服务器崩溃。
至于 socket.io + jQuery,它们确实相处得很好,但它不像 express + jQuery 组合。我会坚持对大多数事情进行 ajax 调用。
node.js + express + jade + stylus + jQuery is my preferred environment.
Using forever to auto restart the server I've never had any real up-time issues even when I have bugs crashing the server on a regular basis.
As for socket.io + jQuery, they do get along fine, but it's just not as natural as the express + jQuery combo. I'd stick to making ajax calls for most things.
Node.JS 仍然有点像狂野西部,但它正在改进。它与 php 编码是一个非常不同的模型,但它非常适合很多网站。您可能希望使用 BackBone.js 之类的东西来使用更胖的客户端来实现瘦服务器(公开 REST API 和您的 websocket 端点),以保持交互干净。
在 Node 中完成所有事情的最大好处是,您将不会在 php 和 js 之间有重复的代码来处理数据库或两者所需的任何其他服务。 Node.JS 在处理大量并发请求方面也非常出色。
祝你好运
Node.JS can still be a little wild west like, but its improving. It is a very different model from coding in php, but it is very well suited for a lot of websites. You'll probably want to do the thin server (expose a REST API and your websocket endpoints) with a fatter client using something like BackBone.js to keep interactions clean.
The big win from doing the whole thing in node is that you will not have duplication of code between php and js for dealing with the DB or any other services required by both. Node.JS is also fantastic at handling tons and tons of concurrent requests.
Good Luck