java Nodejs:在哪里或如何编写复杂的业务逻辑?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7033844/
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
Nodejs: Where or How to write complicated business logic?
提问by Vishal
Recently I got introduced to node.js and cool packages like express and jade. I have few questions consistently knocking my door:
最近我开始接触 node.js 和很酷的包,比如 express 和 jade。我有几个问题一直在敲我的门:
If I pick node.js to build my next website, I will be using JavaScript to write my server-side complicated logic? but I don't think you can compare JavaScript with Java or Python to write server-side code as they have such a vast ocean of libraries. Is node.js really meant for it? or I have missed something?
如果我选择 node.js 来构建我的下一个网站,我将使用 JavaScript 来编写我的服务器端复杂逻辑?但我认为您无法将 JavaScript 与 Java 或 Python 进行比较来编写服务器端代码,因为它们拥有如此庞大的库海洋。node.js 真的适合它吗?或者我错过了什么?
Can I call Java or Python from node.js?
我可以从 node.js 调用 Java 或 Python 吗?
采纳答案by Josh
Not quite sure what most of these folks are talking about.
不太确定这些人中的大多数在谈论什么。
A "vast ocean of libraries" is something the community is actively working on. Check this: http://search.npmjs.org/#/_analytics-- there were 8 packages published yesterday
“浩瀚的图书馆海洋”是社区正在积极努力的事情。检查这个:http: //search.npmjs.org/#/_analytics——昨天发布了 8 个包
Its not going to solve your software design for you. As for where and how to write business logic, many of us embrace mvc or mvvm or something close to it. If you're building an application and like how Rubyists (for example), structure their code you might look at doing something just like that -- aint nobody going to tell you how to structure your code.
它不会为您解决软件设计问题。至于在哪里以及如何编写业务逻辑,我们中的许多人都接受 mvc 或 mvvm 或接近它的东西。如果您正在构建一个应用程序,并且喜欢 Rubyists(例如)如何构建他们的代码,那么您可能会考虑做类似的事情——没有人会告诉您如何构建您的代码。
Check https://github.com/joyent/node/wiki/modules
检查https://github.com/joyent/node/wiki/modules
Some of the more popular libraries for doing the day to day:
一些比较流行的日常工作库:
- Express: http://expressjs.com/- https://github.com/visionmedia/express
- Sinatra inspired, use it to build a typical web app
- Stats: 3407 watchers, 286 forks, on pull request 778
- Compare that to Sinatra itself! 2529 watchers, 366 forks
- With connect, it supports all kinds of middleware:
- sessions,
- all kinds of routing,
- static files
- some 15 different templating engines
- validation, form handling, etc, etc
- Socket.io: http://socket.io/- make it 'real-time'
- DNode: https://github.com/substack/dnode- do rpc between anything
- Backbone.js: http://documentcloud.github.com/backbone/- MVC
- Variety of techniques for re-using your models on the server:
- http://andyet.net/blog/2011/feb/15/re-using-backbonejs-models-on-the-server-with-node/
- Spine.js: http://maccman.github.com/spine.tutorials/index.html- MCV
- Techniques for re-using code on the server:
- http://maccman.github.com/spine.tutorials/node.html
- caolan/async: https://github.com/caolan/async- Help manage your async business logic
- Database, pick your poision
- node_redis, https://github.com/mranney/node_redis- or one of the eight other clients
- "This is a complete Redis client for node.js. It supports all Redis commands"
- node-mysql, https://github.com/felixge/node-mysql- or one of eleven other clients/orms
- node-mongodb-native, https://github.com/christkv/node-mongodb-native
- node-postgres, https://github.com/brianc/node-postgres
- node_redis, https://github.com/mranney/node_redis- or one of the eight other clients
- 快递:http: //expressjs.com/- https://github.com/visionmedia/express
- Sinatra 的启发,用它来构建一个典型的 Web 应用程序
- 统计数据:3407 个观察者,286 个分叉,拉取请求778
- 将其与 Sinatra 本身进行比较!2529个观察者,366个分叉
- 通过connect,它支持各种中间件:
- 会议,
- 各种路由,
- 静态文件
- 大约 15 个不同的模板引擎
- 验证、表单处理等
- Socket.io:http://socket.io/-使“实时”
- DNode:https: //github.com/substack/dnode- 在任何东西之间做 rpc
- Backbone.js:http://documentcloud.github.com/backbone/-MVC
- Spine.js:http://maccman.github.com/spine.tutorials/index.html - MCV
- 在服务器上重用代码的技巧:
- http://maccman.github.com/spine.tutorials/node.html
- caolan/async: https://github.com/caolan/async- 帮助管理你的异步业务逻辑
- 数据库,选择你的毒药
- node_redis,https://github.com/mranney/node_redis- 或其他八个客户端之一
- “这是一个完整的 node.js Redis 客户端。它支持所有 Redis 命令”
- node-mysql,https://github.com/felixge/node-mysql- 或其他十一个客户端/orms 之一
- node-mongodb-native,https://github.com/christkv/node-mongodb-native
- 节点 postgres,https://github.com/brianc/node-postgres
- node_redis,https://github.com/mranney/node_redis- 或其他八个客户端之一
There's also a host of ORMs out there, if thats your bag. Things like http://mongoosejs.com/, http://sequelizejs.com/and friends
如果那是您的包,那么还有许多 ORM。诸如http://mongoosejs.com/、http://sequelizejs.com/和朋友之类的东西
Test-driven development is at the core of node. There are 15 different TDD packages to choose from that range from full code coverage analysis to custom assert modules.
测试驱动开发是 node 的核心。有 15 种不同的 TDD 包可供选择,从完整的代码覆盖率分析到自定义断言模块。
Saying all modules are incomplete is silly. There is an incredibly dedicated group of people building and maintaining tons working open-source in this community every day.
说所有模块都不完整是愚蠢的。每天都有一群非常敬业的人在这个社区中构建和维护大量的开源工作。
There might be reasons to pass over node, but its not for an inactive community or lack of libraries.
可能有理由忽略节点,但这不是因为社区不活跃或缺少库。
回答by brandizzi
I would say you missed something - more specifically, the core purpose of Node.js, that is, the asynchronous I/O model.
我会说你错过了一些东西——更具体地说,Node.js 的核心目的,即异步 I/O 模型。
I started a little pet project to test Node.js - how it "feels" and how to program on it. I became impressed by the ease of working in such ecosystem: Node.js code is easy to write (although its asynchronous paradigm is not that straightforward for the conventional programmer), libraries are easy to build etc. etc. Even npm is amazingly easy: I just found the most straightforward way to provide code of your own as a library is to make a public package of it - and it is absurdly easy!
我开始了一个小项目来测试 Node.js——它“感觉”如何以及如何在它上面编程。在这样的生态系统中工作的便利性给我留下了深刻的印象:Node.js 代码很容易编写(尽管它的异步范式对传统程序员来说并不那么简单),库很容易构建等等。甚至 npm 也非常简单:我刚刚发现将您自己的代码作为库提供的最直接方法是制作它的公共包 - 这非常简单!
However, there is not much good tools to work with Node.js. Maybe because it is tooeasy to do anything, most libraries are partially-implemented, undocumented solutions.
然而,没有太多好的工具可以使用 Node.js。也许是因为做任何事情都太容易了,大多数库都是部分实现的、未记录的解决方案。
Also, note that the relevant difference of Node.js is not the JavaScript language, but the asynchronous I/O model. It is the most interesting aspect of Node.js, but the asynchronous programming style is not as well tested as the conventional way of web development. Maybe it is really the marvel that is propagandized - or perhaps, it is not as good as promised.
另请注意,Node.js 的相关差异不是 JavaScript 语言,而是异步 I/O 模型。这是 Node.js 最有趣的方面,但异步编程风格没有像传统的 Web 开发方式那样经过充分测试。或许,宣传的真的是奇迹——或许,并没有想象中的那么美好。
Even in the case it pays off, will you have enough developers to maintain such an (at least still) unusual codebase? If you can get a lot of advantages from the asynchronous "way of life" of Node.js, you can use more consolidated languages and frameworks, such as Twistedfor Python (which is my preferred languabe, so take care with my opinion :) ). There may be something like this for Java, too. Anyway, I suspect that you do not have a lot of interest in this model for now, since your question focuses more on languages than in the programming paradigm, so Node.js does not have much to offer to you anyway.
即使它得到了回报,您是否有足够的开发人员来维护这样一个(至少仍然是)不寻常的代码库?如果你能从 Node.js 的异步“生活方式”中获得很多优势,你可以使用更统一的语言和框架,比如Twistedfor Python(这是我的首选语言,所以请注意我的意见:) )。Java 也可能有类似的东西。无论如何,我怀疑你现在对这个模型没有太大兴趣,因为你的问题更多地关注语言而不是编程范式,所以 Node.js 无论如何都没有给你提供太多东西。
So... no, I would not develop something professonaly in Node.js for now, although I think it is both fun and instructive to study. You can do it, however - just do not do it without having in mind the main purpose of Node.js: asynchronous-IO, event-driven programming. If it is what you want, Node.js is a good alternative.
所以……不,我暂时不会在 Node.js 中开发一些专业的东西,尽管我认为学习它既有趣又有益。但是,您可以这样做 - 只是不要在没有记住 Node.js 的主要目的的情况下这样做:异步 IO,事件驱动编程。如果这是你想要的,Node.js 是一个不错的选择。
回答by generalhenry
Ryan did not start with JavaScript. A large part of why Node was created in JavaScript is that JavaScript lacked vast oceans of libraries.
Ryan 不是从 JavaScript 开始的。使用 JavaScript 创建 Node 的很大一部分原因是 JavaScript 缺乏海量的库。
Those vast oceans of libraries are almost all written in blocking code.
那些庞大的库海洋几乎都是用阻塞代码编写的。
To take full advantage of Node.js you need to limit your self to non blocking libraries. Which means that might need to write some libraries to complete your project in Node.js.
要充分利用 Node.js,您需要将自己限制在非阻塞库中。这意味着可能需要编写一些库来在 Node.js 中完成您的项目。
回答by Paul Rumkin
Of course, you can use Python, PHP, c++ or other technologies with nodejs 'cuz node can run it as a child process. Nodejs give you the freedom to use any technology which you want inside itself. You can use whatever you want combining the most performance programs.
当然,你可以使用 Python、PHP、c++ 或其他技术与 nodejs 一起使用,因为 node 可以将其作为子进程运行。Nodejs 让你可以自由地使用任何你想要的技术。您可以使用任何您想要的组合性能最高的程序。
回答by Michelle Tilley
I think you'll be surprised by the amount of work you can get done in JavaScript via Node.js. There are a bunch of librariesavailable for Node and more are being written all the time. Furthermore, native extensionsare also available for those times where you might need to drop down to a lower-level.
我想你会惊讶于你可以通过 Node.js 在 JavaScript 中完成的工作量。这里有一堆库可用于节点和更是被写入所有的时间。此外,本机扩展也可用于您可能需要降级到较低级别的时候。
If you think there's a gap where Node won't be able to provide for your business logic, take a look around NPM or give Google a quick serch to see if anyone else has already solved your problem.
如果您认为 Node 无法提供您的业务逻辑,请查看 NPM 或快速搜索 Google,看看是否有其他人已经解决了您的问题。
回答by sym3tri
There are some things that JavaScript just can't do. If you come up against those Node might not be the best choice for your app. However you can probably accomplish most of what you need.
有些事情是 JavaScript 做不到的。如果您遇到这些 Node 可能不是您应用程序的最佳选择。但是,您可能可以完成大部分您需要的工作。
As far as the API being limited, I suggest you take a look at npm and all the librariesin its repository. Specifically ones like underscore.js. Many aim to fill in the gaps of what native JavaScript lacks compared to other languages.
至于 API 的限制,我建议您查看 npm及其存储库中的所有库。特别是像underscore.js 这样的。许多旨在填补原生 JavaScript 与其他语言相比所缺乏的空白。