Zeit is now called Vercel, and this tutorial might be outdated
Zeit现在称为Vercel ,并且本教程可能已过时
One of the most simple ways to deploy a Node.js application is through the Now platform created by Zeit.
部署Node.js应用程序最简单的方法之一就是通过Zeit创建的Now平台。
Now 2 was recently launched. This tutorials focuses on that. There are many differences from Now 1, highlighted in this post.
现在推出了2。 本教程着重于此。 与这篇文章中突出显示的 Now 1有很多区别。
Now makes the deployment and distribution step of an app very, very simple and fast. You can think of it as the “cloud”, as you don’t really know where your app will be deployed, but you know that you will have a URL where you can reach it.
现在,使应用程序的部署和分发步骤变得非常非常简单和快捷。 您可以将其视为“云”,因为您并不真正知道应用程序的部署位置,但是您知道将拥有一个可以访问它的URL。
You can use Now to deploy Node.js apps, Static Websites, and much more.
您可以使用Now来部署Node.js应用程序,静态网站等。
Now does not only support Node.js (also Go, PHP, Python and other languages), but in this tutorial I’ll just consider this technology.
现在,它不仅支持Node.js(还支持Go,PHP,Python和其他语言),但在本教程中,我将仅考虑该技术。
Now is free to start using, with generous free plan that includes free SSL, 100GB of hosting, 1000 serverless functions invocations per day, 1000 builds per month, 100GB of bandwidth per month, and the use of the global CDN. The pricing page helps get an idea of the costs if you need more.
现在可以免费开始使用了免费的免费计划,其中包括免费的SSL,100GB的托管,每天1000次无服务器功能调用,每月1000次构建,每月100GB带宽以及使用全局CDN 。 定价页面可帮助您了解所需的成本。
安装 (Installation)
The best way is to install Now Desktop, which you can download from https://github.com/zeit/now-desktop. It’s an Electron application which also installs the Now CLI, a tool we’ll later use.
最好的方法是安装Now Desktop ,您可以从https://github.com/zeit/now-desktop下载。 这是一个Electron应用程序,还安装了Now CLI (我们稍后将使用的工具)。
Through that, you can deploy applications using a simple drag and drop interface, really handy!
这样,您可以使用简单的拖放界面来部署应用程序,非常方便!
Tip: you can also just install the command line if you prefer, from https://zeit.co/download
提示:如果愿意,您也可以只从https://zeit.co/download安装命令行。
After starting, enter your email and Now will go on with the authentication, sending you a verification email.
开始后,输入您的电子邮件,Now将继续进行身份验证,向您发送验证电子邮件。
![](https://i-blog.csdnimg.cn/blog_migrate/99ff9c84eb7953f7312366e80b52ea49.png)
Once you are signed in, you can follow the quick tutorial:
登录后,您可以按照快速教程进行操作:
![](https://flaviocopes.com/zeit-now/Screen%20Shot%202018-11-08%20at%2019.16.39.png)
After you scroll through the few screens, the app is moved to the menu bar, and you when you click it, shows the activity feed:
滚动浏览几个屏幕后,该应用程序将移至菜单栏,单击时会显示活动供稿:
![](https://i-blog.csdnimg.cn/blog_migrate/967fc32fb2c0a8bcfdcef9fe2dc9367c.png)
As you can see in this image, I installed Now a few months back with that email account, but didn’t do much on it.
正如您在该图中所看到的,几个月前,我使用该电子邮件帐户安装了Now,但是并没有做太多事情。
After signing in with the desktop application, the command line application called now
is automatically signed in as well.
与桌面应用程序签名后,被称为命令行应用程序now
在和自动签名。
Jump into the terminal, and run now
.
跳转到终端 ,并运行now
。
Tip: if you don’t need/want the desktop client, you can also just install the
now
CLI using the commandnpm install -g now
. Then, you’ll proceed to login usingnow login
.提示:如果不需要/不需要桌面客户端,也可以使用命令
npm install -g now
安装now
CLI。 然后,您将继续使用now login
。
立即部署应用程序 (Deploying an application on Now)
For “application” we can just think about a simple HTML file, or a complex application with many build steps.
对于“应用程序”,我们可以考虑一个简单HTML文件,或者一个具有许多构建步骤的复杂应用程序。
Whatever your app is, you can run
无论您的应用是什么,都可以运行
now
in a folder, and that folder will be uploaded to the cloud.
在一个文件夹中,该文件夹将被上传到云中。
With just one caveat: the folder must contains a now.json
file with (at least) this content:
请注意,该文件夹必须包含一个now.json
文件,该文件至少具有以下内容:
{
"version": 2
}
to make sure the projects runs on Now 2.
确保项目在Now 2上运行。
Once you run the now
program, the app is deployed to a random URL under the now.sh
domain. In this case, it’s https://test-8h57iozp1.now.sh
and I just deployed an index.html
file with <p>test</p>
in its content:
运行now
程序后,该应用程序将部署到now.sh
域下的随机URL。 在这种情况下,它是https://test-8h57iozp1.now.sh
,我刚刚部署了index.html
文件,其内容为<p>test</p>
:
![](https://i-blog.csdnimg.cn/blog_migrate/290a2e7d16ed7fae77e03dc707ac29bb.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c9e5ce9fe66bed387720c39d4d8afb78.png)
After the deploy, the Now Desktop app lists this activity
部署后,Now Desktop应用程序将列出此活动
![](https://i-blog.csdnimg.cn/blog_migrate/7053e222f4d90ca42c825e9e1be8c883.png)
If you now change that index.html file content and run again now
, you’ll get a different URL for your app.
如果你现在更改index.html文件的内容,并再次运行now
,你会得到你的应用程序不同的URL。
![](https://flaviocopes.com/zeit-now/Screen%20Shot%202018-11-09%20at%2009.56.54.png)
This might be unexpected, right? Before it was test-8h57iozp1.now.sh
, now it’s test-m2vcwrsc8.now.sh
. And both URLs can be accessed. Why?
这可能是意外的,对吧? 之前是test-8h57iozp1.now.sh
,现在是test-m2vcwrsc8.now.sh
。 并且两个URL都可以访问 。 为什么?
![](https://i-blog.csdnimg.cn/blog_migrate/760dc7f242b42b652f8153a87f0cd6e9.png)
The expected behavior should be that the old URL is updated with the new content, but it’s not.
预期的行为应该是用新的内容更新旧的URL,但事实并非如此。
Now has this concept of immutability that has many advantages, including the option of testing multiple releases at the same time, multiple developers working on different parts of an app, rollbacks, and more.
现在有了不变性的概念,它具有许多优点,包括可以同时测试多个版本,多个开发人员在应用程序的不同部分上工作,回滚等等。
In production, or when you want to share your app, you need a fixed URL. It can’t change every time you update it, right? To do this, you create an alias:
在生产环境中或要共享应用程序时,需要一个固定的URL。 每次更新都无法更改,对吗? 为此,您创建一个别名 :
now alias test-m2vcwrsc8.now.sh test-flavio
![](https://i-blog.csdnimg.cn/blog_migrate/409f1bcf4dd111a0659e76e75f59f5a0.png)
After you run this command, test-flavio.now.sh
will point to that deploy.
运行此命令后, test-flavio.now.sh
将指向该部署。
Every time you want to update the deploy this alias points to, you run the command again. In this way you can freely test drive new releases, and when you are ok with making it the official one, you update the alias.
每次您要更新此别名指向的部署时,都再次运行命令。 通过这种方式,您可以自由地测试驱动器的新发行版,如果可以将其正式发布,则可以更新别名。
Same thing to rollback to a previous deploy, you just alias to the old deploy unique URL.
回滚到以前的部署也一样,只是别名为旧的部署唯一URL。
To remove a deployment, run now rm <URL>
:
要删除部署,请立即运行now rm <URL>
:
now rm test-m2vcwrsc8.now.sh
配置Lambda函数 (Configure a lambda function)
We can execute a Node.js application on demand when visiting a particular URL.
访问特定URL时,我们可以按需执行Node.js应用程序。
For example add a test.js
file with this content:
例如,添加具有以下内容的test.js
文件:
module.exports = (req, res) => {
res.end(`Hi!`)
}
In order to make it executable, we must add a build step to now.json
:
为了使其可执行,我们必须在now.json
添加一个构建步骤:
{
"version": 2,
"builds": [{ "src": "test.js", "use": "@now/node" }]
}
Head to https://test-a0onzajf4.now.sh/test.js to see the result (“Hi!”)
前往https://test-a0onzajf4.now.sh/test.js查看结果(“嗨!”)
![](https://flaviocopes.com/zeit-now/Screen%20Shot%202018-11-09%20at%2011.32.59.png)
The curious thing is that now the index.html
file does not load any more like before. This is because the default build step is overwritten, so we need to add one to fix this:
奇怪的是,现在index.html
文件不再像以前那样加载。 这是因为默认构建步骤已被覆盖,因此我们需要添加一个步骤来解决此问题:
![](https://i-blog.csdnimg.cn/blog_migrate/3d7f0b050a30e752e176b3d9832bf107.png)
Add the line { "src": "index.html", "use": "@now/static" }
to our build:
将以下行添加到我们的构建中: { "src": "index.html", "use": "@now/static" }
:
{
"version": 2,
"builds": [
{
"src": "test.js",
"use": "@now/node"
},
{
"src": "index.html",
"use": "@now/static"
}
]
}
![](https://i-blog.csdnimg.cn/blog_migrate/4a8ab5ceccf7a90e848176c19d31597c.png)
从这往哪儿走 (Where to go from here)
There’s lots more to find out about Now, but this tutorial will hopefully get you started in the right direction.
关于Now,还有很多东西可以找到,但是本教程有望使您朝正确的方向开始。
Some useful resources for you are
对您有用的一些资源是