Zeit Now tutorial

Learn how to use the Now platform created by Zeit

Zeit is now calledWiesel, And this tutorial may be outdated

One of the easiest ways to deploy a Node.js application is throughright nowPlatform createdZeitgeist.

Now 2 has started. This tutorial focuses on this. There are many differences from Now 1Highlight in this article.

Now, the deployment and distribution steps of the application have become very, very simple and fast. You can think of it as a "cloud" because you don't really know where the application is deployed, but you know that you will have a URL where you can access it.

You can use Now to deploy Node.js applications, static websites, etc.

Now, it not only supports Node.js (it also supports Go, PHP, Python and other languages), but in this tutorial, I will only consider that technology.

It’s free to get started now, the free generous plan includes free SSL, 100GB hosting, 1000ServerlessPerform function calls every day, build 1000 times a month, 100GB bandwidth per month and use globalCDN. ThisPricing pageIf you need more, it can help you understand the cost.

installation

The best way is to installDesktop nowFrom which you can downloadhttps://github.com/zeit/now-desktop. This is anelectronicThe app will also installCLI now, The tools we will use later.

In this way, you can use a simple drag-and-drop interface to deploy applications, which is very convenient!

Tip: You can also install the command line from the command line according to your needshttps://zeit.co/download

After you start, enter your email, Now will continue to verify your identity and send you a verification email.

After logging in, you can follow the quick tutorial:

After scrolling through a few screens, the application will move to the menu bar, and when you click the menu, you will display the active feed:

As you can see in the picture, I installed Now with this email account a few months ago, but didn't do much.

After logging in using the desktop application, the command line application is namednowIt will also be automatically logged in.

jump intoterminalAnd then runnow.

Tip: If you don’t need/do not need a desktop client, you can just install itnowCLI use commandnpm install -g now. Then you will continue to log in usingnow login.

Deploy the application now

For "applications", we can consider a simple HTML file, or a complex application with many construction steps.

No matter what your application is, it can run

now

In a folder, the folder will be uploaded to the cloud.

Please note that the folder must contain anow.jsonThe file has (at least) the following content:

{
  "version": 2
}

to make sure the projects runs on Now 2.

Once you runnowProgram, then deploy the application tonow.shfield. under these circumstanceshttps://test-8h57iozp1.now.shI just deployedindex.htmlWith file<p>test</p>In its content:

After deployment, the Now Desktop app will list this activity

If you change the content of the index.html file now and run it againnow, You will get aDifferent URLFor your application.

This may be unexpected, right? Before thistest-8h57iozp1.now.sh,right nowtest-m2vcwrsc8.now.sh. withTwo URLs can be accessed. why?

The expected behavior should be to update the old URL with new content, but this is not the case.

Now there is this conceptImmutabilityIt has many advantages, including the option to test multiple versions at the same time, multiple developers working on different parts of the application, rollbacks, etc.

In a production environment or when you want to share an application, you need a fixed URL. You can’t change it every time you update, right? To do this, you need to create aAlias:

now alias test-m2vcwrsc8.now.sh test-flavio

After running this command,test-flavio.now.shWill point to that deployment.

Every time you want to update the deployment pointed to by this alias, run the command again. In this way, you are free to test the new release of the drive, and if it can be officially released, you can update the alias.

Rolling back to the previous deployment is the same, but the alias is the unique URL of the old deployment.

To delete the deployment, runnow rm <URL>:

now rm test-m2vcwrsc8.now.sh

Configure Lambda function

When accessing a specific URL, we can execute the Node.js application on demand.

For example, add atest.jsFiles containing the following content:

module.exports = (req, res) => {
  res.end(`Hi!`)
}

In order to make it executable, we must add a build step to 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!”)

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:

Add the line { "src": "index.html", "use": "@now/static" } to our build:

{
  "version": 2,
  "builds": [
    {
      "src": "test.js",
      "use": "@now/node"
    },
    {
      "src": "index.html",
      "use": "@now/static"
    }
  ]
}

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.

Some useful resources for you are


More services tutorials: