简体   繁体   English

设置后如何将Express.js用于新的Web应用程序?

[英]How do I use Express.js for a new web app after setup?

我已经为一个hello world应用程序安装了Express,并且运行良好。现在我想创建一个新应用程序,如何使用已安装的Express用于该新应用程序,而不是使用以下方法为该新应用程序重新安装它: npm install express每次创建新应用时都可以从Internet重新安装它?

Use npm install -g express 使用npm install -g express

But it worths adding express to your package.json with the rest of (future) dependencies you will need, so you can type npm install in your project's root and it will automatically install all the dependecies with the specified version and so on. 但是值得在您的package.json中添加express和其他所需的(未来)依赖项,因此您可以在项目的根目录中键入npm install,它将自动安装具有指定版本的所有依赖项,依此类推。

npm install express

...will install Express only into the current folder path that you have in your terminal. ...将仅将Express安装到终端中的当前文件夹路径中。 If you want to install the package for all Node.js instances, you'll need to run: 如果要为所有 Node.js实例安装该软件包,则需要运行:

npm -g install express 

or, depending on your server's security model, 或者,根据您服务器的安全模型,

sudo npm -g install express

Sometimes you'll need to link the package if the linking failed (you'll get a "Cannot find module X" error), via: 有时,如果链接失败,您将需要链接软件包(您将收到“无法找到模块X”错误),方法是:

sudo npm link express

If you want to read more about it, this blog post is a good read. 如果您想了解更多信息, 这篇博客文章是不错的阅读。

Don't install Express globally 不要在全球范围内安装Express

Locally installing any package that you're going to depend on is generally considered a best practice in the Nodejs community. 在本地安装要依赖的任何软件包通常被认为是Nodejs社区中的最佳实践。 It comes down to managing dependencies. 它归结为管理依赖项。

See: Nodejs Blog - NPM 1.0: Global vs Local Installation 请参阅: Nodejs博客-NPM 1.0:全局安装与本地安装

Consider the following scenario: 请考虑以下情形:

Lets say you do a global install of Express for your first project. 假设您为第一个项目进行了Express的全局安装。 You're start off using the latest version of the library and everything goes well. 您从使用该库的最新版本开始,一切顺利。 Then over time you write 10 more applications that depend on that install. 然后随着时间的推移,您还要编写10个依赖于该安装的应用程序。 Eventually Express hits the next main version and adds some killer features but they've also introduced a few backwards-incompatible API changes. 最终,Express发行了下一个主要版本,并添加了一些杀手级功能,但它们还引入了一些向后不兼容的API更改。 You'll want to use the latest version for a new project but a global update will probably break all of the previous applications you've created. 您需要为新项目使用最新版本,但是全局更新可能会破坏您创建的所有先前应用程序。

In a best case scenario, you'll have 100% test coverage on all your old projects and through hard work and determination you will eventually manage to update/fix everything that broke with the update. 在最佳情况下,您将对所有旧项目进行100%的测试覆盖范围,并通过艰苦的工作和决心,最终将能够更新/修复因更新而中断的所有内容。

Realistically, nobody has 100% test coverage on everything and it's likely that something the update broke will be missed and accidentally pushed into production. 实际上,没有人对所有内容都进行100%的测试覆盖,并且可能会错过更新失败的内容,并意外地将其投入生产。 ::cringe:: ::畏缩::

The scenario I just outlined is what lots of people refer to as dependency hell. 我刚刚概述的场景是很多人称为依赖地狱。 It's a common reason why some organizations get locked into a specific version of a framework/application/dll. 这是某些组织被锁定在特定版本的Framework / Application / dll中的常见原因。

With nodejs, it's cheap and easy to handle dependencies individually for each project. 使用nodejs,可以轻松便宜地轻松处理每个项目的依赖关系。 Modules generally aren't as monolithic (read huge) like the frameworks you'd expect in other languages. 模块通常不像您期望的其他语言框架那样单片(阅读大量)。

To install Express locally with dependencies just use: 要使用依赖项在本地安装Express,请使用:

npm install express --save

Note: The --save flag will automatically add Express and the version number to your package.json file. 注意:--save标志将自动将Express和版本号添加到package.json文件中。 If you want the module marked under the devDependencies listing instead, use the --save-dev flag. 如果要在devDependencies列表下标记模块,请使用--save-dev标志。

The exception to the rule: 规则的例外:

The exception to the don't install locally rule is CLI applications. 不在本地安装”规则的例外是CLI应用程序。 It's rare that someone will write code that depends on a CLI application and -- even if they do -- CLI apps only superficially expose the highest-order functions. 很少有人会写依赖于CLI应用程序的代码,而且即使他们这样做,CLI应用程序也只是表面地公开了最高阶的函数。 Unless the CLI has a development API that you're project depends on, it's probably safe and more convenient to install the package globally. 除非CLI具有您要依赖的开发API,否则在全局范围内安装该软件包可能更安全,更方便。

Aside: A library developer's perspective 旁白:图书馆开发人员的观点

As libraries are updated and improved it's not uncommon for library devs to change the API between major versions (ex 1.0, 2.0, 3.0) as they get a better feel for how the everything should be structured. 随着库的更新和改进,库开发人员在主要版本(例如1.0、2.0、3.0)之间更改API的情况并不少见,因为他们可以更好地理解所有内容的结构。 When backwards-incompatible changes are introduced it's not uncommon for people to get all finger-pointy and start bickering about 'poor design'. 当引入向后不兼容的更改时,人们常常会指责并开始对“糟糕的设计”争论不休。 Most of those issues have little to do with the design of the libraries being used. 这些问题大多数与所使用的库的设计无关。 Rather, they're a cause of poor design and version management from the devs that implement them. 相反,它们是导致实现它们的开发人员设计和版本管理不佳的原因。

The truth of the matter is, it's impossible foresee a best possible design for a library until most of the code has already been implemented and put to use by a larger community. 事情的真相是,在大多数代码已经由较大的社区实现并投入使用之前,不可能预见图书馆的最佳设计。 The best projects are those that grow organically, have a large userbase providing lots of valuable feedback, and adapt over time to their user's needs. 最好的项目是那些有机增长的项目,拥有庞大的用户群,可提供大量有价值的反馈,并随着时间的推移适应其用户的需求。

Major versions are usually the most exciting time for library devs because that's where we actually get to release ground-breaking changes. 对于图书馆开发人员而言,主要版本通常是最激动人心的时刻,因为这是我们真正发布重大突破的地方。 All the versions in between only serve to exist for boring maintenance and bugfixes. 介于两者之间的所有版本仅用于无聊的维护和错误修正。

One of the greatest benefits of Nodejs is it's small core. Nodejs的最大好处之一就是它的小核心。 The Javascript language itself is well defined so there's little/no chance that updates to the core will break any code. JavaScript语言本身定义明确,因此几乎没有/没有机会更新内核会破坏任何代码。 The second greatest benefit of Nodejs is that the package manager along with the common package.json project file format make managing dependency versions as easy and straightforward as possible. Node.js的第二大好处是,程序包管理器以及通用的package.json项目文件格式使管理依赖版本变得尽可能简单和直接。

Source: I develop libraries and spend an obscene amount of time thinking about good API design. 资料来源:我开发了库,并花了大量的时间思考良好的API设计。

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

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