You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
Copy file name to clipboardExpand all lines: components/references-mdx/runtimes/official-runtimes/official-runtimes.mdx
+24-1Lines changed: 24 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,7 +77,9 @@ If you need more advanced behavior, such as a custom build step or private npm m
77
77
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`.
78
78
79
79
<Notetype="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.
81
83
</Note>
82
84
83
85
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.
256
258
257
259
The Go Runtime will automatically detect a `go.mod` file to install dependencies at the root of a project.
258
260
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.
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
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.
30
31
31
32
For example, the following would live in `api/hello.js`:
32
33
@@ -50,9 +51,9 @@ Example deployment: <https://node-api.now-examples.now.sh/api/hello?name=reader>
50
51
/>
51
52
<Caption>The Serverless Function with the <InlineCode>name</InlineCode> query parameter using Node.js to change the name.</Caption>
52
53
53
-
### Node.js TypeScript support:
54
+
### Using TypeScript
54
55
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.
56
57
57
58
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:
58
59
@@ -77,174 +78,20 @@ You can install the `@now/node` module for type definitions through npm:
77
78
<Snippetdarktext="npm i -D @now/node" />
78
79
<Caption>Installing the <InlineCode>@now/node</InlineCode> module for Type definitions of <InlineCode>NowRequest</InlineCode> and <InlineCode>NowResponse</InlineCode></Caption>
79
80
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.
81
82
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:
|`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
-
} elseif (req.query.who) {
123
-
who =req.query.who
124
-
} elseif (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:
|`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 (typeofreq.body!=='string') {
158
-
returnres.status(400).text('Invalid request')
159
-
}
160
-
161
-
if (req.body!=='ping') {
162
-
returnres.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
-
constasciify=require('asciify-image')
189
-
190
-
module.exports=async (req, res) => {
191
-
if (!req.query.username) {
192
-
returnres.status(400).send('username is missing from query parameters')
<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
-
<Notetype="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).
239
86
240
87
---
241
88
242
89
## Go
243
90
244
91
**File Extension**: `.go`<br />
245
-
**Default Runtime Version**: Go 1.x
92
+
**Default Version**: Go 1.x
246
93
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.
248
95
249
96
For example, the following would live in `api/date.go`:
250
97
@@ -271,58 +118,18 @@ func Handler(w http.ResponseWriter, r *http.Request) {
271
118
272
119
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>
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.
326
133
327
134
For example, the following would live in `api/date.py`:
328
135
@@ -344,9 +151,37 @@ class handler(BaseHTTPRequestHandler):
344
151
345
152
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>
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.newdo |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>
0 commit comments