𝚫 now: 瞬间部署你的 Node.js 应用

4,105 阅读3分钟
原文链接: zeit.co
 ~/my-proj $ ls
package.json   index.js    lib   static
 ~/my-proj $ now
> Ready! https://my-proj-hj1v2m.now.sh
  (copied to clipboard) [440ms]
> Upload [====================] 100% 5.7s
> Sync complete (1.38MB) [5702ms]
 ~/my-proj $ 
https://my-proj-hj1v2m.now.sh

What's now?

𝚫 now allows you to take your JavaScript (Node.js[1]) powered websites, applications and services to the cloud with ease, speed and reliability.

In practical terms, any directory that contains a package.json file can be transported to the cloud with one command: now

Every time you deploy a project, 𝚫 now gives you a unique URL to it (even before build processes are complete!). These URLs look like this: my-app-sknncqwaoc.now.sh

When it's time to take your deployment to production, you simply pick an appropriate alias.

You can think of 𝚫 nowCDN for dynamic code (microservices and backends).

Get started

Installation

To get started using now, install it from npm:

$ npm install -g now

When the installation is finished, it'll ask for your email to identify you.
Click the email you receive, and you'll be automatically logged-in.

If you need to change your login or re-authenticate, run

$ now --login

Your first project

On a terminal, run:

$ mkdir /my-project
$ cd /my-project

It's important that your project has a package.json file.
Within it, you have to define a start script:

{
    "name": "my-project",
    "version": "0.0.1",
    "dependencies": {
        "express": "4.13.4"
    },
    "scripts": {
        "start": "node index.js"
    }
}

Your index.js for this example could look as follows:

const app = require('express')();
app.get('/', (req, res) => {
    res.send('Welcome');
});
app.listen();

NOTE:npm start has to listen on a port. It can be any port

To deploy with now, run:

$ now

You'll get a link (copied to your clipboard by default) that you can share
immediately with your peers, even before upload and startup completes.

Features

Easy

𝚫 now only requires Node.JS to be installed in your computer.

  • No need to install git or source control
  • No need to setup keys
  • No complicated cloud provider setup or registration

Unlimited

Every time your run now, you get a fresh URL that represents the current state of your application.

  • No need to remove old ones
  • No setup of applications or projects
  • URLs stay around forever

Realtime

As soon as you type now, we give you a URL that you can go to or share with co-workers and collaborators.

This URL will show the progress of your deployment. You might see files uploading, and then we show you the progress of the commands executed to deploy your program.

As a matter of fact, now is so fast that you might not even get to see this in many cases!

Future-proof

𝚫 now exposes servers that speak the HTTP protocol. We don't impose any new propritary Cloud APIs. You can run your apps -with our without 𝚫 now- with OSS software.

In addition:

  • All your traffic is served over HTTP/2
  • Traffic is only served over secure connections.

Cloud View Source

If you add /_src to any now URL (example[2]), you'll see the code behind it. You and your team will be able to quickly understand what's behind your production systems.

/_src?f=package.json&h=8;10

my-project

my-project-fchpzewmak.now.sh
{
  "name": "reallybigone",
  "version": "0.0.1",
  "devDependencies": {
    "gulp": "3.9.0"
    "gulp-autoprefixer": "2.3.1",
    "gulp-sourcemaps": "1.5.2",
    "serve": "1.4.0"
    "node-sass": "3.4.2"
  },
  "scripts": {
    "start": "serve -p 3000 ."
  }
}

What's more, every character and line of code is hyperlinkable. This makes collaborative issue triaging and debugging a breeze!

Pricing

ENTERPRISE PLAN
  • Pay as you go
  • No limits
  • Multi-region

FAQ

Does this scale?

𝚫 now's approach to scalability is unique. Here's what makes it special:

Dynamic

We don't under-allocate or over-allocate resources.
You don't have to ever adjust any nobs, configure instances or set up processes.

Multi-cloud.

We don't depend on a single specific cloud provider, but abstract them instead.

This means that we can always find the best combination of cost, performance, reliability and resistence to failure or censorship.

Why not AWS Lambda / Azure Functions / Google Cloud Functions ?

Exposing just a function is not enough

In real-world applications the metadata of the request is of particular importance.

The response might vary, for example, according to the capabilities of the client. If the User-Agent is so, or the Accept header is such. These won't be automatically present in your payloads, so you'll have to manually supply them in each function call.

The other missing feature is the response code. Instead of returning 404 when something is not found, 403 when the permissions are not met or 500 when something goes wrong, you'll end up creating a new ad-hoc codec for errors that only your own system understands.

It leads to lock-in

The way most cloud providers address the problems described above is by introducing a context object that has access to information from the environment.

The problem is that this context object varies ever so slightly from provider to provider. Were this to be standarized, it would look like HTTP.

We serve all your apps over HTTP/2

HTTP/2 has two critical features that make your requests effectively as lean as "micro functions", from a bandwidth and efficiency perspective:

  • Multiplexing. HTTP/2 exhibits great latency characteristics and bandwidth use byre-utilizing the same TCP connection for all communications. This provides a great advantage for mobile users specially.
  • Header compression. Headers that are the same over the lifetime of a connection are not sent multiple times.

What version of Node.js do you run?

We run Node.JS 5.10.1 by default. We plan to support customizing it with the engines field.

How do I run my build process?

To define a script that has to be run everytime before install, specify a build script in package.json

{
  "name": "my-project",
  "dependencies": {
    "gulp": "3.9.1",
    "serve": "3.8.3"
  },
  "scripts": {
    "build": "gulp",
    "start": "serve ."
  }
}

How does my app detect 𝚫 now

If you want to define a specific task for now that overrides build, you can define a task called now-buildnow-build is set, build is ignored.

If you want to have a specific task for startup, you can specify now-start, and the regular start task will be ignored.

In addition, the NOW env variable is exposed to the tasks and scripts.

Can I host static websites?

Absolutely! Just use a Node program to serve your files, such as serve[4]
package.json can look like this:

{
  "name": "my-project",
  "dependencies": {
    "serve": "3.8.3"
  },
  "scripts": {
    "start": "serve ."
  }
}

Who's behind 𝚫 now

The team that built now has experience deploying and running the largest Node.JS deployments.

Our first Node.JS version in production was 0.1.100, and since then we've built some of the most popular and enduring open-source modules and frameworks, such as:

  • socket.io[5] the most popular realtime framework (35M downloads
  • mongoose[6] the most popular MongoDB JavaScript ODM (7M downloads