Skip to content

Commit ec3d4c9

Browse files
timothyisMatthew Sweeney
andcommitted
Update Supported Languages to be shorter and easier to read (#1538)
* Update Supported Languages to be shorter and easier to read * Update pages/docs/v2/serverless-functions/supported-languages.mdx Co-Authored-By: Matthew Sweeney <[email protected]> * Improve grammar Co-authored-by: Matthew Sweeney <[email protected]>
1 parent 30b7edf commit ec3d4c9

File tree

2 files changed

+69
-211
lines changed

2 files changed

+69
-211
lines changed

components/references-mdx/runtimes/official-runtimes/official-runtimes.mdx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ If you need more advanced behavior, such as a custom build step or private npm m
7777
When creating a new project, it automatically receives the latest LTS Node.js version available on ZEIT Now. In order to understand which Node.js version your project is using, log the output of `process.version`.
7878

7979
<Note type="warning">
80-
Just recently, our upstream providers have deprecated support for Node.js 8. In turn, existing deployments using this version have to be upgraded to either version 10 or 12, as described above.
80+
Just recently, our upstream providers have deprecated support for Node.js 8.
81+
In turn, existing deployments using this version have to be upgraded to either
82+
version 10 or 12, as described above.
8183
</Note>
8284

8385
The available Node.js versions are either **10** or **12** and can be configured by defining an `engines` property in a `package.json` file, like in the example below:
@@ -256,6 +258,27 @@ Go version 1.x is used for Go projects deployed with ZEIT Now.
256258

257259
The Go Runtime will automatically detect a `go.mod` file to install dependencies at the root of a project.
258260

261+
### Go Build Configuration
262+
263+
You can provide custom build flags by using the `GO_BUILD_FLAGS` [build environment variable](/docs/v2/build-step/#using-environment-variables-and-secrets).
264+
265+
```json
266+
{
267+
"build": {
268+
"env": {
269+
"GO_BUILD_FLAGS": "-ldflags '-s -w'"
270+
}
271+
}
272+
}
273+
```
274+
275+
<Caption>
276+
An example <InlineCode>-ldflags</InlineCode> flag with{' '}
277+
<InlineCode>-s -w</InlineCode>. This will remove debug information from the
278+
output file. This is the default value when no{' '}
279+
<InlineCode>GO_BUILD_FLAGS</InlineCode> are provided.
280+
</Caption>
281+
259282
---
260283

261284
## Python

pages/docs/v2/serverless-functions/supported-languages.mdx

Lines changed: 45 additions & 210 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const meta = {
1010
title: 'Supported Languages for Serverless Functions',
1111
description: 'Learn how to deploy Serverless Functions with ZEIT Now.',
1212
editUrl: 'pages/docs/v2/serverless-functions/supported-languages.mdx',
13-
lastEdited: '2019-12-04T20:07:25.000Z'
13+
lastEdited: '2020-01-07T13:00:14.000Z'
1414
}
1515

1616
Within the `/api` directory of your projects, ZEIT Now will automatically recognize the languages listed on this page, through their file extensions, and serve them as Serverless Function.
@@ -20,13 +20,14 @@ Within the `/api` directory of your projects, ZEIT Now will automatically recogn
2020
- [Node.js](#node.js)
2121
- [Go](#go)
2222
- [Python](#python)
23+
- [Ruby](#ruby)
2324

2425
## Node.js
2526

2627
**File Extensions**: `.js`, `.ts`<br />
27-
**Default Runtime Version**: 10.x (or [defined](#defined-node.js-version))
28+
**Default Version**: 12.x (or [defined](/docs/runtimes#official-runtimes/node-js/node-js-version))
2829

29-
Node.js files through a JavaScript file or a TypeScript file within the `api` directory, containing a default exported function, will be served as a Serverless Function.
30+
Node.js files through a JavaScript file or a [TypeScript](#using-typescript) file within the `api` directory, containing a default exported function, will be served as Serverless Functions.
3031

3132
For example, the following would live in `api/hello.js`:
3233

@@ -50,9 +51,9 @@ Example deployment: <https://node-api.now-examples.now.sh/api/hello?name=reader>
5051
/>
5152
<Caption>The Serverless Function with the <InlineCode>name</InlineCode> query parameter using Node.js to change the name.</Caption>
5253

53-
### Node.js TypeScript support:
54+
### Using TypeScript
5455

55-
Deploying a Node.js function with the `.ts` extension will automatically be recognized as a TypeScript file and compiled to a Serverless Function.
56+
Deploying a Node.js function with the `.ts` extension will automatically be recognized as a [TypeScript](https://www.typescriptlang.org/) file and compiled to a Serverless Function.
5657

5758
As an example; a file called `hello.ts` in the `api` directory, and importing types for the ZEIT Now platform Request and Response objects from the `@now/node` module:
5859

@@ -77,174 +78,20 @@ You can install the `@now/node` module for type definitions through npm:
7778
<Snippet dark text="npm i -D @now/node" />
7879
<Caption>Installing the <InlineCode>@now/node</InlineCode> module for Type definitions of <InlineCode>NowRequest</InlineCode> and <InlineCode>NowResponse</InlineCode></Caption>
7980

80-
You can also define a `tsconfig.json` to configure the ZEIT Now TypeScript compiler:
81+
You can also define a `tsconfig.json` to configure the ZEIT Now TypeScript compiler.
8182

82-
```json
83-
{
84-
"compilerOptions": {
85-
"module": "commonjs",
86-
"target": "esnext",
87-
"sourceMap": true,
88-
"strict": true
89-
}
90-
}
91-
```
92-
93-
<Caption>
94-
An example <InlineCode>tsconfig.json</InlineCode> file.
95-
</Caption>
96-
97-
### Node.js Request and Response Objects
98-
99-
For each request of a Node.js Serverless Function, two objects, request and response, are passed to it. These objects are the standard HTTP [request](https://nodejs.org/api/http.html#http_event_request) and [response](https://nodejs.org/api/http.html#http_class_http_serverresponse) objects given and used by Node.js, but they include extended helpers provided by ZEIT Now:
100-
101-
#### Node.js Helpers
102-
103-
<br />
104-
105-
| method | description | object |
106-
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
107-
| `req.query` | An object containing the request's [query string](https://en.wikipedia.org/wiki/Query_string), or `{}` if the request does not have a query string. | request |
108-
| `req.cookies` | An object containing the cookies sent by the request, or `{}` if the request contains no cookies. | request |
109-
| `req.body` | An object containing the body sent by the request, or `null` if no body is sent. | request |
110-
| `res.status(code)` | A function to set the status code sent with the response where `code` must be a valid [HTTP status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes). Returns `res` for chaining. | response |
111-
| `res.send(body)` | A function to set the content of the response where `body` can be a `string`, an `object` or a `Buffer`. | response |
112-
| `res.json(obj)` | A function to send a JSON response where `obj` is the JSON object to send. | response |
113-
114-
The following example showcases the use of `req.query`, `req.cookies` and `req.body` helpers:
115-
116-
```js
117-
module.exports = (req, res) => {
118-
let who = 'anonymous'
119-
120-
if (req.body && req.body.who) {
121-
who = req.body.who
122-
} else if (req.query.who) {
123-
who = req.query.who
124-
} else if (req.cookies.who) {
125-
who = req.cookies.who
126-
}
127-
128-
res.status(200).send(`Hello ${who}!`)
129-
}
130-
```
131-
132-
<Caption>
133-
Example Node.js function using the <InlineCode>req.query</InlineCode>,{' '}
134-
<InlineCode>req.cookies</InlineCode> and <InlineCode>req.body</InlineCode>{' '}
135-
helpers. It returns greetings for the user specified using{' '}
136-
<InlineCode>req.send()</InlineCode>.
137-
</Caption>
138-
139-
##### `req.body`
140-
141-
We populate the `req.body` property on the request object with a parsed version of the content sent with the request when it is possible.
142-
143-
We follow a set of rules on the `Content-type` header sent by the request to do so:
144-
145-
| `Content-Type` header | Value of `req.body` |
146-
| ----------------------------------- | --------------------------------------------------------------------------------------- |
147-
| No header | `undefined` |
148-
| `application/json` | An object representing the parsed JSON sent by the request. |
149-
| `application/x-www-form-urlencoded` | An object representing the parsed data sent by with the request. |
150-
| `text/plain` | A string containing the text sent by the request. |
151-
| `application/octet-stream` | A [Buffer](https://nodejs.org/api/buffer.html) containing the data sent by the request. |
152-
153-
With the `req.body` helper, you can build applications without extra dependencies or having to [parse the content of the request manually](https://nodejs.org/ja/docs/guides/anatomy-of-an-http-transaction/#request-body). The following example inspects `req.body`, if it is 'ping', it will return 'pong'.
154-
155-
```js
156-
module.exports = (req, res) => {
157-
if (typeof req.body !== 'string') {
158-
return res.status(400).text('Invalid request')
159-
}
160-
161-
if (req.body !== 'ping') {
162-
return res.status(400).text('The value expected is `ping`')
163-
}
164-
165-
res.status(200).send('pong')
166-
}
167-
```
168-
169-
<Caption>
170-
An example Node.js function using the <InlineCode>req.body</InlineCode> helper
171-
that returns <InlineCode>pong</InlineCode> when you send{' '}
172-
<InlineCode>ping</InlineCode>.
173-
</Caption>
174-
175-
### Node.js Async Support
176-
177-
ZEIT Now supports asynchronous functions out-of-the-box.
178-
179-
In this example, we use the package `asciify-image` to create [ascii art](https://en.wikipedia.org/wiki/ASCII_art) from a person's avatar on GitHub. First, we need to install the package:
180-
181-
```
182-
npm i --save asciify-image
183-
```
184-
185-
In our code, we export an asynchronous function and we take advantage of the [helpers](#node.js-helpers).
186-
187-
```js
188-
const asciify = require('asciify-image')
189-
190-
module.exports = async (req, res) => {
191-
if (!req.query.username) {
192-
return res.status(400).send('username is missing from query parameters')
193-
}
194-
195-
const avatarUrl = `https://github.com/${req.query.username}.png`
83+
### Advanced Node.js Usage
19684

197-
const opts = { fit: 'box', width: 40, height: 40 }
198-
const asciified = await asciify(avatarUrl, opts)
199-
200-
return res.status(200).send(asciified)
201-
}
202-
```
203-
204-
<Caption>An example showcasing the use of an asynchronous function.</Caption>
205-
206-
### Node.js Dependency Installation
207-
208-
The installation of dependencies behaves as follows:
209-
210-
- If a `package-lock.json` is present, `npm install` is used
211-
- Otherwise, `yarn` is used.
212-
213-
### Defined Node.js Version
214-
215-
The default Node.js version used is **10.x** but can be changed to **12.x** by defining an `engines` property in a `package.json` file like in the example below:
216-
217-
```json
218-
{
219-
"engines": {
220-
"node": "12.x"
221-
}
222-
}
223-
```
224-
225-
<Caption>
226-
Defining the <InlineCode>engines</InlineCode> property in a{' '}
227-
<InlineCode>package.json</InlineCode> file.
228-
</Caption>
229-
230-
When defining an `engines` property, there are two different values that can be supplied, **10.x** - the default if the property is not defined - or **12.x**.
231-
232-
The `.x` portion of the `engines` value is required by our upstream providers and cannot be changed.
233-
234-
<Note type="warning">
235-
The only options that can be passed are <InlineCode>10.x</InlineCode> or{' '}
236-
<InlineCode>12.x</InlineCode>, passing any other value will cause your build
237-
to fail.
238-
</Note>
85+
For more advanced usage of Node.js on the ZEIT Now platform, including [information about the request and response objects](/docs/runtimes#official-runtimes/node-js/node-js-request-and-response-objects), [extended helpers](/docs/runtimes#official-runtimes/node-js/node-js-request-and-response-objects) for Node.js on Now, [private npm packages](/docs/runtimes#advanced-usage/advanced-node-js-usage/private-npm-modules-for-node-js), or [advanced configuration](/docs/runtimes#advanced-usage/advanced-node-js-usage), see [the Node.js runtime documentation](/docs/runtimes#official-runtimes/node-js).
23986

24087
---
24188

24289
## Go
24390

24491
**File Extension**: `.go`<br />
245-
**Default Runtime Version**: Go 1.x
92+
**Default Version**: Go 1.x
24693

247-
Go files in the `api` directory that export a function matching the `net/http` Go API will be served as a Serverless Function.
94+
Go files in the `api` directory that export a function matching the `net/http` Go API will be served as Serverless Functions.
24895

24996
For example, the following would live in `api/date.go`:
25097

@@ -271,58 +118,18 @@ func Handler(w http.ResponseWriter, r *http.Request) {
271118

272119
When deployed, the example function above will be served as a Serverless Function, returning the latest date. See it live with the following link: <https://go-api.now-examples.now.sh/api/date>
273120

274-
### Private Packages for Go
275-
276-
To install private packages with `go get`, define `GIT_CREDENTIALS` as a [build environment variable](/docs/v2/build-step).
277-
278-
All major Git providers are supported including GitHub, GitLab, Bitbucket, as well as a self-hosted Git server.
121+
### Advanced Go Usage
279122

280-
With GitHub, you will need to [create a personal token](https://github.com/settings/tokens) with permission to access your private repository.
281-
282-
```json
283-
{
284-
"build": {
285-
"env": {
286-
"GIT_CREDENTIALS": "https://username:[email protected]"
287-
}
288-
}
289-
}
290-
```
291-
292-
<Caption>
293-
An example build environment variable using a GitHub personal token for
294-
Private Go Packages.
295-
</Caption>
296-
297-
### Go Build Configuration
298-
299-
You can provide custom build flags by using the `GO_BUILD_FLAGS` [build environment variable](/docs/v2/build-step/#using-environment-variables-and-secrets).
300-
301-
```json
302-
{
303-
"build": {
304-
"env": {
305-
"GO_BUILD_FLAGS": "-ldflags '-s -w'"
306-
}
307-
}
308-
}
309-
```
310-
311-
<Caption>
312-
An example <InlineCode>-ldflags</InlineCode> flag with{' '}
313-
<InlineCode>-s -w</InlineCode>. This will remove debug information from the
314-
output file. This is the default value when no{' '}
315-
<InlineCode>GO_BUILD_FLAGS</InlineCode> are provided.
316-
</Caption>
123+
For more advanced usage of Go on the ZEIT Now platform, including [dependencies](/docs/runtimes#official-runtimes/go/go-dependencies), [build configuration](/docs/runtimes#official-runtimes/go/go-build-configuration), [private dependencies](/docs/runtimes#advanced-usage/advanced-go-usage), or [advanced configuration](/docs/runtimes#advanced-usage/advanced-go-usage), see [the Go runtime documentation](/docs/runtimes#official-runtimes/go).
317124

318125
---
319126

320127
## Python
321128

322129
**File Extension**: `.py`<br />
323-
**Default Runtime Version**: Python 3.6
130+
**Default Version**: Python 3.6
324131

325-
Python files within the `api` directory, containing an handler variable that inherits from the `BaseHTTPRequestHandler` class or an `app` variable that exposes a WSGI or ASGI application, will be served as a Serverless Function.
132+
Python files within the `api` directory, containing an handler variable that inherits from the `BaseHTTPRequestHandler` class or an `app` variable that exposes a WSGI or ASGI application, will be served as Serverless Functions.
326133

327134
For example, the following would live in `api/date.py`:
328135

@@ -344,9 +151,37 @@ class handler(BaseHTTPRequestHandler):
344151

345152
When deployed, the example function above will be served as a Serverless Function, returning the current date and time. See it live with the following link: <https://python-api.now-examples.now.sh/api/date>
346153

347-
### Python Dependencies
154+
### Advanced Python Usage
155+
156+
For more advanced usage of Python on the ZEIT Now platform, including [dependencies](/docs/runtimes#official-runtimes/python/python-dependencies), [WSGI applications](/docs/runtimes#advanced-usage/advanced-python-usage/web-server-gateway-interface), [ASGI applications](/docs/runtimes#advanced-usage/advanced-python-usage/asynchronous-server-gateway-interface), or [advanced configuration](/docs/runtimes#advanced-usage/advanced-python-usage), see [the Python runtime documentation](/docs/runtimes#official-runtimes/python).
157+
158+
---
159+
160+
## Ruby
161+
162+
**File Extension**: `.rb`<br />
163+
**Default Version**: Ruby 2.5.5
164+
165+
Ruby files that define a singular HTTP handler, within the `api` directory, will be served as Serverless Functions.
166+
167+
For example, the following would live in `api/date.rb`:
168+
169+
```rb
170+
Handler = Proc.new do |req, res|
171+
time = Time.new
172+
res.status = 200
173+
res['Content-Type'] = 'text/plain'
174+
res.body = "Current Time: " + time.inspect
175+
end
176+
```
177+
178+
<Caption>An example Ruby function that returns the current date.</Caption>
179+
180+
When deployed, the example function above will be served as a Serverless Function, returning the current date and time. See it live with the following link: <https://ruby-date.now.sh/api/date>
181+
182+
### Advanced Ruby Usage
348183

349-
ZEIT Now supports installing dependencies for Python defined in the `requirements.txt` file or a `Pipfile.lock` file at the root of the project.
184+
For more advanced usage of Ruby on the ZEIT Now platform, including [dependencies](/docs/runtimes#official-runtimes/ruby/ruby-dependencies), or [Rack interfaces](/docs/runtimes#advanced-usage/advanced-ruby-usage/rack-interface), see [the Ruby runtime documentation](/docs/runtimes#official-runtimes/ruby).
350185

351186
---
352187

0 commit comments

Comments
 (0)