Function API
Function Template
Every cloud function needs module.exports an async function, this function contains two variables: params and context. E.g:
module.exports = async function(params, context) {
return {
message: 'Hi, AirCode.',
};
}
module.exports = async function(params, context) {
return {
message: 'Hi, AirCode.',
};
}
params
The request data when accessing functions. It may represent different data depending on the request method.
POST Parameters
{Object | string | Buffer}:When initializing a POST,params represents the content in the Request Body,and the concrete type is decided by Content Type:
| Content Type | Params Data Type |
|---|---|
application/json | Object |
multipart/form-data | Object |
text/plain | string |
application/x-www-form-urlencoded | Object |
Others(e.g. application/octet-stream) | Buffer |
Guide
GET Parameters
{Object}:When initializing a GET,params is a key-value object converted by Query String.
Tips
The value in the object is of string or Array<string> type, for example params.a === '1' when ?a=1.
Guide
context
The context of the request, also contains some helper methods.
context.headers
const contentType = context.headers['content-type'];
const myCustomHeader = context.headers['x-my-header'];
const contentType = context.headers['content-type'];
const myCustomHeader = context.headers['x-my-header'];
{Object}:Get the headers of the HTTP request in the form of key-value pairs.
Tips
The keys in the object are all lowercase, for example, it should be context.headers['content-type'], not context.headers['Content-Type'].
context.method
{string}:Get the method of the HTTP request, the value is all uppercase, for example 'POST'、'GET'。
context.request
{Request}:Node.js request object.
context.req
Abbreviation for context.request.
context.response
{Response}:Node.js response object.
context.res
Abbreviation for context.response.
context.path
// /hello?hi=aircode
const path = context.path;
// => /hello
// /hello?hi=aircode
const path = context.path;
// => /hello
{string} Get the complete path of the HTTP request.
context.url
// /hello?hi=aircode
const url = context.url;
// => /hello?hi=aircode
// /hello?hi=aircode
const url = context.url;
// => /hello?hi=aircode
{string} Get the complete URL of the HTTP request, including the path and query string.
context.protocol
{string} Get the protocol of the HTTP request, which is https in AirCode.
context.query
// ?hello=world&abc=xyz&abc=123
const hello = context.query.hello;
// => 'world'
const abc = context.query.abc;
// => [ 'xyz', '123' ]
// ?hello=world&abc=xyz&abc=123
const hello = context.query.hello;
// => 'world'
const abc = context.query.abc;
// => [ 'xyz', '123' ]
{Object}: Get the query String converted key-value object from the HTTP request, same as params in GET request.
Tips
The value in the object is of string or Array<string> type, for example context.query.a === '1' when ?a=1.
context.cookies
{Object}: Get the cookies of the HTTP request in the form of key-value pairs.
Tips
The value in the object is of string type, you should parse JSON string if client stored a stringify string in the cookie.
context.trigger
{string}: Obtain the source of the function call, can be one of the following values:
| Value | Trigger | Guide |
|---|---|---|
'HTTP' | via HTTP/HTTPS | Invoke Functions |
'SCHEDULE' | via scheduled tasks | Scheduled Tasks |
'DEBUG' | via online debugging | Debug Online |
context.set(field, value)
context.set('content-type', 'application/json');
context.set('x-abc-header', 'hello world');
context.set('content-type', 'application/json');
context.set('x-abc-header', 'hello world');
Set the returned HTTP Response Headers.
Parameter
{string} field: the key of the Response Header to set{string} value: The value of the Response Header to set
context.remove(field)
context.remove('x-abc-header');
context.remove('x-abc-header');
Delete the HTTP Response Header corresponding to the field.
Parameter
{string} field: the key of the Response Header to delete
context.setCookie(name, value[, options])
context.setCookie('token', 'aircode', { expires: new Date(Date.now() + 24 * 60 * 60 * 1000), httpOnly: true });
context.setCookie('token', 'aircode', { expires: new Date(Date.now() + 24 * 60 * 60 * 1000), httpOnly: true });
Sets a cookie named name with the value value.
Parameter
{string} name: The name of the cookie to be set.{string} value: The value to be set for the cookie.{object} options: Cookie options.
For specific options details, please refer to express response cookie.
context.clearCookie(name[, options])
context.clearCookie('token', { path: '/admin' });
context.clearCookie('token', { path: '/admin' });
Clears the cookie value of the cookie named name.
Parameter
{string} name: The name of the cookie to be cleared.{object} options: Cookie options.
context.redirect(url[, code])
context.redirect('https://aircode.io');
context.redirect('https://aircode.io');
Forces a redirection to the specified URL with the status code, defaulting to 302.
Parameter
{string} url: The URL to redirect to.{number} code: The HTTP Status Code to use for the redirect, with a default value of302.
context.status(code)
context.status(201);
context.status(201);
Tips
By default, the Status Code returned by the function is 200 for successful execution and 500 for failed execution.
Used to set the returned HTTP Status Code.
Parameter
{number} code: The HTTP Status Code to set. You can find all codes at Wikipedia - List of HTTP status codes.