简体   繁体   English

Node-App-Architecture:node.js中表达的地方

[英]Node-App-Architecture: where does express come into node.js

I'm trying to teach myself how to use nodejs and building a webApp with it. 我正在尝试教自己如何使用nodejs并使用它构建一个webApp。 In most examples I come around expressJs is used as webFrameWork. 在大多数示例中,我将expressJs用作webFrameWork。 I tried to read about it, but I mostly find examples. 我试图阅读有关它的内容,但我大多会找到示例。 I want to understand where exactly expressJs comes in when using nodejs. 我想了解使用nodejs时expressJs 确切出现在哪里。

I see how to create a helloworld-web-server with 'pure' node-js ( http://howtonode.org/hello-node ). 我看到了如何使用“纯” node-js( http://howtonode.org/hello-node )创建helloworld-web-server。

I also see how to make this with expressJs: http://expressjs.com/starter/hello-world.html 我还看到了如何使用expressJs实现这一点: http ://expressjs.com/starter/hello-world.html

Is express simply kind of a wrapper to be able to avoid that http-boilerplate code? 表达只是一种包装可以避免该http-boilerplate代码? If so, to what extent should I use express and to which should I be using nodes-API? 如果是这样,我应该在多大程度上使用express?在什么程度上应该使用nodes-API? Is it simply used to do the routing? 它只是用于路由吗? Or what else is there? 或者还有什么?

In the Express-FAQs I saw it doesn't help to take care of the database and that no models are created using expressJs. Express-FAQ中,我看到照顾数据库并没有使用expressJ创建模型的问题。 So, when building an App with node and express how should I handle the data (users, posts, whatever,...)? 因此,在构建带有节点的App并表达我应如何处理数据(用户,帖子等)时?

I tried to do a whole lot of reading, but I'm not having a clear big-picture at which point one layer of abstraction ends and where the next exactly starts. 我试图进行大量阅读,但是我没有清晰的全局视图,在这一点上一层抽象结束,下一层完全开始。 I mean I understand that expressJs is a webFrameWork that sits on the node webserver running on a V8-engine, however I am a bit confused of what the final app code should be architectured: do I kind of have intermixed nodeJs code with expressJs code in a single app.js? 我的意思是我知道expressJs是一个位于V8引擎上运行的节点Web服务器上的webFrameWork,但是我对最终应用程序代码应构建的结构有些困惑:我是否在其中混用了nodeJs代码和expressJs代码?一个app.js? Or should I have specific files which require express and another one for the database which does not require it? 还是我应该有需要Express的特定文件,而又有不需要的特定文件?

So now you understand what I mean with: Where does express come into node.js? 因此,现在您明白我的意思了: node.js在哪里表达表达? What does it do for me which I don't get from 'pure' node and what should I not expect from it? 我不能从“纯”节点得到什么,对我有什么作用,我不应该从中得到什么?

Maybe my whole confusion comes because the whole thing is javascript and you don't have like these explicit 'language-barriers' which make things a bit more obvious (I'm thinking about the LAMP-stack for instance). 也许我整个困惑是因为整个事情都是javascript,而您却没有像这些明确的“语言障碍”那样使事情变得更加明显(例如,我正在考虑LAMP堆栈)。 Or is this exactly the way I'm supposed to think about javascript in general? 还是这正是我一般应该考虑javascript的方式? Like the only thing which you actually get is a javascript-interpreter and everything else is lib that we can include and and expressJs simply has nodeJs as a dependency? 就像您真正获得的唯一东西是一个javascript解释器,其他所有东西都是我们可以包含的lib,而expressJs只是将nodeJs作为依赖项? I'm really sorry for being so fuzzy. 真是太模糊了,我真的很抱歉。 I'm just trying to get my mental-model straight. 我只是想弄清楚我的心理模型。

Is express simply kind of a wrapper to be able to avoid that http-boilerplate code? 表达只是一种包装可以避免该http-boilerplate代码?

Yes

If so, to what extent should I use express and to which should I be using nodes-API? 如果是这样,我应该在多大程度上使用express?在什么程度上应该使用nodes-API?

All Request and Response handling should be done via Express. 所有请求和响应处理都应通过Express完成。 Also see how Express Router simplifies things when there are a lot of urls to be served in your app 另请参阅当您的应用中有很多网址要投放时,Express Router如何简化事情

Is it simply used to do the routing? 它只是用于路由吗? Or what else is there? 或者还有什么?

Yes that's pretty much it. 是的,仅此而已。 But that's the point. 但这就是重点。 Do one thing but do it good. 做一件事,但做得很好。

So, when building an App with node and express how should I handle the data (users, posts, whatever,...)? 因此,在构建带有节点的App并表达我应如何处理数据(用户,帖子等)时?

Take a look at Loopback 看看环回

I am a bit confused of what the final app code should be architectured 我对最终的应用程序代码应该架构什么有些困惑

You (like myself) seem to be coming from a J2EE / Spring or Dot NET world where the various frameworks try to address the complete landscape of 2 tier or 3 tier architecture view - model - dao. 您(像我一样)似乎来自J2EE / Spring或Dot NET世界,其中各种框架试图解决2层或3层体系结构视图-模型-dao的完整情况。

Here, the landscape is little different. 在这里,景观几乎没有什么不同。 The frameworks are designed on the principle that do only one thing and do it good. 框架是根据只做一件事并且做好事的原则设计的。 So the overall solution becomes much tougher to arrive at. 因此,总体解决方案变得更加困难。

So now there are 所以现在有

  1. Multiple independent frameworks (node packages) available addressing the same problem space and 提供多个独立框架(节点程序包)来解决相同的问题空间和
  2. All these components have to work together to achieve the final big-picture solution. 所有这些组件都必须协同工作,以实现最终的全面解决方案。

So there are no crisp boundaries of how the stitching of these packages will be done. 因此,如何包装这些包装没有明确的界限。 Its fluid. 它的流动性。 And its totally dependent on your peculiar problem. 它完全取决于您的特殊问题。 So no generic solutions here and no clear boundaries of what will end where and what will begin where. 因此,这里没有通用的解决方案,也没有明确的界限,什么将在何处结束,什么将从何处开始。

You have to decide (and that's hard work). 您必须决定(这是艰苦的工作)。 This decision making will be a process where you evolve your architecture instead of creating it upfront in a big bang effort. 该决策将是一个过程,您可以在其中逐步发展体系结构,而不是大刀阔斧地预先创建体系结构。

It will be met with multiple failures and frustrations in initial days/months. 在最初的几天/几个月中,将会遇到许多失败和挫折。

And then read about Javascript Fatigue 然后阅读有关Javascript疲劳的信息

I'm just trying to get my mental-model straight. 我只是想弄清楚我的心理模型。

But you are not alone. 但是你并不孤单。 :) Mine isn't straight either. :)我的也不是直人。 Not many people in the industry have it straight I can assure you. 我可以向您保证,业内没有多少人能直言不讳。

Node itself basically lets you write Javascript on the server, and not much else. 节点本身基本上可以让您在服务器上编写Javascript,而其他方面则很少。 It has some basic HTTP functions, but I wouldn't want to use those functions alone to make a web app. 它具有一些基本的HTTP功能,但我不想单独使用这些功能来制作Web应用程序。 Express is like the Rails equivalent for the Node ecosystem (with less magic). Express类似于Node生态系统的Rails等效项(具有更少的魔力)。 It handles routing, cookies, and most of your other web-related stuff. 它处理路由,Cookie和大多数其他与Web相关的内容。

You might want to check out Connect , the de facto standard library for middleware. 您可能想查看Connect ,它是中间件的事实上的标准库。 As for data modeling, most people in the Node world use mongodb with Mongoose . 至于数据建模,Node世界中的大多数人都使用mongodb和Mongoose In practice, you'll end up using Node built-in functions where it makes sense, and plugging in small libraries where you need other functionality. 在实践中,最终将使用有意义的Node内置函数,并在需要其他功能的地方插入小型库。 If you have any other questions, I'm happy to help. 如果您还有其他疑问,我们很乐意为您提供帮助。

In essence, Node is just a JavaScript interpreter, packaged with a bunch of helper libraries and a command line interface tool. 从本质上讲,Node只是一个JavaScript解释器,打包有许多帮助程序库和命令行界面工具。 Node is packaged with a bunch of utility libraries for performing common development tasks, like interacting with a file system or sending an HTTP request. Node打包有许多实用程序库,用于执行常见的开发任务,例如与文件系统进行交互或发送HTTP请求。 Connect is an abstraction on-top of Node, providing a middleware layer. Connect是Node之上的抽象,提供了一个中间件层。 Express is an abstraction on-top of Connect & Node , that makes it easier to build certain types of applications, primarily web applications that use http, websockets, complex routing, session and cookie management, authentication, etc. ExpressConnectNode的抽象概念,它使构建某些类型的应用程序变得更加容易,这些应用程序主要是使用http的Web应用程序,websocket,复杂的路由,会话和cookie管理,身份验证等。

There are some competitors at the 'same level of abstraction' as Express — one of the more popular is Hapi (very similar to express), and if you are looking for a Rails-like framework on-top of Node, then look at SailsJS, Locomotive, or Meteor. 有一些与Express处于“相同抽象级别”的竞争者-最受欢迎的竞争者之一是Hapi (非常类似于express),如果您正在Node之上寻找类似Rails的框架,请查看SailsJS ,机车或流星。

Express speeds up your development effort and helps to better structure the layout of the code for you. Express加快了您的开发工作,并帮助您更好地构建代码的布局。 If you are new to node.js, you can build an express server in no time with minimum effort. 如果您不熟悉node.js,则可以毫不费力地立即构建快速服务器。 It is also easily extendible. 它也很容易扩展。 Usually people use a few others popular node-modules in conjunction with express-- connect, passport, ejs, body-parser, connect-redis, mongoose etc depending on what your usage scenarios are and what integration you have with other servers. 通常,人们根据使用场景以及与其他服务器的集成情况,将一些其他流行的节点模块与express,connect,passport,ejs,body-parser,connect-redis,mongoose等结合使用。

As an analogy: 打个比方:

express is to node as expressnode

  • asp.net is to .NET asp.net就是.NET
  • j2ee is to java j2ee是Java
  • ... ...

Express is just server side middleware framework implemented in javascript executed by the V8 javascript engine on the server side. Express只是由服务器端V8 javascript引擎执行的javascript中实现的服务器端中间件框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM