加载中

Node.js may still be young relative to its other language counterparts, but when it comes to hosting there are a lot of options. In this post we’ll take a look at several “Platform as a Service” providers.

I’m not including “Infrastructure as a Service” options like AWS and Joyent, although the lines between PaaS and IaaS get a little blurry with some of these options.

In this round, I’m primarily looking at two aspects: deploying and configuring environment variables. I’ll also include some notes about getting started, some screenshots of dashboards, and other miscelaneous observations. In future posts we’ll run some basic performance testing and take a look at the ease of scaling.

相对其他竞争语言来说,Node.js算是挺年轻的,但随着它逐渐成熟,现在有大把的Node.js托管平台可供我们选择。在这篇文章里我会对几个PaaS服务提供商做一个简单的评测。

文章里并没有包含IaaS的服务商比如AWS或者Joyent,尽管下文中提到的几个选择在概念上有点模糊,既可以被算作PaaS也可以被算作IaaS。

本文主要关注两个方面:部署配置环境变量。同时为方便新手,文中也提供了一些提示、操作面板的截图以及其他的杂项内容。以后的文章中我们会进行一些基本的性能测试,并且评测一下各个平台是否易于扩展。


The Players

Provider Deployment method Environment variables Performance
Nodejitsu CLI CLI or web interface coming soon
Heroku git CLI
Modulus CLI or web upload CLI or web interface
App Fog CLI CLI or web interface
Azure CLI or git CLI or web interface
dotCloud CLI CLI or .yml file
Engine Yard git ey_config npm module
OpenShift git SSH and create file
CloudFoundry coming soon coming soon

The Setup

I’m starting with a very simple Express app, and using nconf to elegantly handle the varying ways that we’ll be specifying the port our app should listen to (sometimes required) and a dummy variable I’m calling SECRET. It will first look for arguments passed in to the node command, then environment variables, then it will try to load a config.json file one level up from the root, and finally fall back on some defaults we’ve defined right in the code. When I load the app, I’ll be able to tell if our variables are being correctly pulled from one of those external sources. If it’s not, the response I get back when I load the app will be default secret. I should also be able to see what port the app is listening on an the NODE_ENV it’s using if I can access the logs from the startup of the app.

Finally, I’m setting"engines": { "node": "v0.10.x" ...in the package.json file to see how each provider reacts.

And now in no particular order…

参赛选手

服务提供商 部署方法 如何配置环境变量 性能
Nodejitsu CLI CLI/web界面 马上就搞
Heroku git CLI
Modulus CLI/网页上传 CLI/web界面
App Fog CLI CLI/web界面
Azure CLI/git CLI/web界面
dotCloud CLI CLI/.yml文件
Engine Yard git ey_config npm模块
OpenShift git SSH + 创建文件
CloudFoundry 马上就搞 马上就搞

安装过程

我开始于一个简单的Express应用,并使用了nconf以便简洁的用一个配置处理多个平台上不同的监听端口设置方法(有时候是必须要设置的),同时加入了一个傀儡变量SECRET。nconf会首先查找传给node命令的参数,之后查找环境变量,然后尝试加载根目录上一级的config.json文件,最后回退到我们在代码中设置的默认值。通过这个过程,在应用被加载时,我就能了解到我们的设置有没有从以上的几个外部来源中被正确的读取到。如果没读到,那么加载应用的响应值就会是默认的SECRET。如果我能获取应用启动后的日志,我还能确定应用所监听的端口号——它被记在NODE_ENV环境变量中。

最后,我在package.json文件中设置了这个对象:"engines": { "node": "v0.10.x" …,以观察各平台的不同反应。

以下排名不分先后……

Nodejitsu

https://www.nodejitsu.com/

One of the original players and still purely a Node.js solution, Nodejitsu became an official partner of Joyent’s back when Joyent dropped their no.de service (shame, it was such an awesome domain name). Nodejitsu no longer has a permanently free tier, but individual plans start at a paltry $3 a month and there’s a 30-day free trial.

Configuring variables

According to the docs it doesn’t matter what you set the listening port to, as long as it’s either 80 or greater than 1024.

Setting our SECRET to override the default was straightforward. You can use the CLI or web interface to list and set variables, much like several other providers on this list.

Nodejitsu

https://www.nodejitsu.com/

作为首批提供Node.js托管服务的提供商之一,Nodejitsu成了Joyent的官方合作伙伴——在Joyent终止了他们的no.de服务之后(搞毛,这域名多牛掰)。Nodejitsu现在没有永久免费服务了,但是他们提供一个每月$3的廉价独立方案,还有一个30天的免费试用。

配置变量

根据文档所说,你可以用任何你想用的端口号,只要是80端口或者大于1024的端口就行。

用我们自己的SECRET设置去覆盖默认值很容易。可以用CLI或者web界面来搞,和其他几个本次一同参测的平台一样。

Deploying

Pushing your code to the Nodejitsu cloud is done via a custom command-line interface application (CLI), installed with npm. When you sign up you get dumped right into a github repository with instructions, but overall the setup process was pretty painless. You’re prompted to select a subdomain, which is then automatically added to the package.json file. In the couple of tests I ran, deploying was really quick. It auto-increments the version property in the package.json file with each deploy, which doesn’t bother me but might annoy some folks.

I ran into three small hitches. The first was with versioning. In the message that gets spit out upon deploying, I was shown:

info: jitsu v0.12.10-2, node v0.10.4

and yet, I was told that 0.10.x was not a supported value. Only by bumping down to 0.8.x was I able to find success.

Second, I tried to change the name property in package.json and it then wouldn’t let me deploy.

Third, my ENV variables got wiped out every time I re-deployed. Perhaps there’s a way to avoid that.

部署过程

将你的代码推送到Nodejitsu云平台需要通过一个特制的命令行应用(CLI),可以通过npm安装。注册之后你会马上被扔到一个附带安装指令的github repo,虽然这点还算好——但总的来说安装过程还是挺蛋疼的。你得选择一个子域名,然后它会被自动添加到package.json文件里。从我做过的几个测试来看,部署还是很快的。系统会自动增加package.json里的version属性的值,虽然这对我来说无所谓,但有的人可能会觉得很烦。

整个过程中有三个小挫折。第一个是版本问题。部署过程中系统提示如下信息:
info: jitsu v0.12.10-2, node v.10.4
同时人家告诉我系统还不支持0.10.x,必须得用0.8.x版本才行。
第二个是如果我修改了package.json文件里的name属性,系统就不让我部署。
第三个是每回我重新部署的时候,我自己设置的ENV变量都会被抹掉。没准有个办法能解决这问题(译者注:但显然作者没找到)。

Misc Notes and Dashboard

I like that Nodejitsu is so Node.js centric. Any custom configuration is at least handled via the standard package.json file. You can even define custom predeploy and postdeploy hooks. My totally subjective response is that it felt very snappy to deploy and view logs.

Nodejitsu dashboard

杂记和操作面板截图

Nodejitsu以Node.js为中心这一点让我尤为称道,反正自定义设置是通过标准的package.json文件来处理的。你甚至可以自定义部署前(predeploy)和部署后(postdeploy)的钩子(hook)。我个人感觉部署应用和查看日志都挺方便的。Nodejitsu dashboard

Heroku

https://www.heroku.com/

The 800 pound gorilla of the PaaS world, made insanely popular by their undying love from Ruby on Rails enthusiasts everywhere.

Configuring variables

Setting our SECRET to override the default was again using the CLI. No surprises there.

All apps run on port 5000, so you need to be listening for that.

Finally, you have to create a Procfile that has specifies web: node server.js. Not a big deal, but certainly a side effect of running on a PaaS that supports multiple languages.

Heroku

https://www.heroku.com/

PaaS世界的庞然大物, 使用无所不在的Ruby on Rails 构造出的极端流行的平台。

变量配置

同样是使用CLI设置我们的SECRET,用来覆盖默认值。

所有应用在端口5000上运行,所以你需要监听这个端口。

最后,需要创建一个Procfile来说明web应用:node server.js。这只是支持多语言的PaaS的平台的小小的副作用。

Deploying

The Heroku “toolbelt” CLI is used to manage your account and applications, but deploying is done via git. You just add the endpoint they provide you as a remote in your git config. Because they’re not exclusively focused on Node.js, I was pleasantly surprised to find that they already supported v 0.10.6!

My first deployment seemed to succeed, but tracking down the errors I received lead me to discover that I first needed to specify how many resources I wanted devoted to this app:

heroku ps:scale web=1

After that, it was smooth sailing.

部署过程

虽然Heroku用"toolbelt" CLI来管理你的账号和应用,但部署过程得用git来完成。你只需要把他们给你的endpoint作为remote加入到你的git配置里去就行了。由于他们不光提供Node.js服务,所以当我发现他们居然支持v0.10.6的时候我真的很兴奋!

我第一次的部署看起来是成功了,但随后收到的错误信息迫使我去研究一开始得给这个应用配置多少资源才行:
heroku ps:scale web=1
之后就一切顺利了。

Misc Notes and Dashboard

I didn’t try Heroku for my own projects until about 3 months ago, partly because I have a modest level of comfort in setting up my own servers, and partly because I just figured they treated node.js as an afterthought. But if you can get over their lack of WebSocket support and some misleading marketing and stats, it’s a pretty smooth experience.

They’ve also got a very polished and functional dashboard, with some handy features you won’t find elsewhere like pointing to an S3-hosted file for 404 pages, and the ability to transfer ownership of a project to a different user.

Heroku dashboard

杂记和控制面板截图

三个月之前我才第一次尝试在Heroku上搞我自己的项目,一方面是因为我对设置自己的服务器多多少少有点偏执,另一方面是因为Heroku把node.js作为一种追加方案来提供(译者注:也就是Node.js不是Heroku最重要的产品)。不过如果能忽略不支持WebSocket以及有误导性的市场策略和统计信息这俩情况,那这也能算是一次如丝般顺滑的体验。

他们下了大力气弄出来这个既漂亮又实用的控制面板,还附带几个很方便的特性(你在别处一般找不到),比如设置某个S3托管的文件作为自定义的404页面,以及向其他用户转移项目所有权的功能。

Heroku dashboard

Modulus

http://modulus.io

The .io extension should tip you off that this is a relatively new service. Focused just on Node.js, they have built-in support for MongoDB and local file storage.

Configuring variables

Variables can be set either via the web interface or the CLI. I got stuck trying to create the SECRET. After a bit of trial and error, I discovered that you can’t include spaces in the value! So modulus secret didn’t work. Weird.

The app needs to listen on port 8080, “but we recommend using the PORT environment variable (process.env.PORT).”

Modulus

http://modulus.io

这个.io后缀名说明了这是个比较年轻的平台。他们专注于Node.js,并且内置对MongoDB和本地文件存储的支持。

配置变量

变量既可以用web界面也可以用CLI配置。当进行到创建SECRET这一步时我被卡住了。试了几次以后我发现变量的值里不能包括空格!结果在Modulus平台上我没法自定义SECRET。我醉了……囧。

应用必须监听8080端口,但是官方说“我们推荐您使用PORT环境变量( process.env.PORT )。”

Deploying

Deploying can be done via CLI, but you can also zip your whole project and upload it via their web interface, which is… interesting. I didn’t have any problems deploying, but your entire project (except node_modules) gets bundled and uploaded every time, which makes it a much slower process than tools that use the “diff” capabilities of git or rsync.

As of writing, Modulus runs v 0.8.15 and ignores whatever you specify in package.json

Misc Notes and Dashboard

I’m cheering for these guys and hope they continue to improve. It’s nice having the option of built-in MongoDB, and I like the upfront pricing.

Modulus dashboard

部署

部署可以通过CLI来做,但你也可以压缩整个项目并把它通过他们很有趣的网络接口上传。我没有任何问题,部署,但每次要把你整个项目(除了node_modules之外)都打包和上传,这会使它的处理过程比那些使用 git 的“diff”功能或 rsync 的工具慢得多。

至于写作,模块运行于0.8.15版本,并忽略你在 package.json 中指定的任何内容。

其它注意事项和仪表板

我为这些家伙欢呼,并希望他们能继续改善。有个内置MongoDB的选项会是不错的,我喜欢前期定价。

Modulus dashboard

返回顶部
顶部