JSON API Server

Create an HTTP API server capable of responding with a JSON object

Create an HTTP API server capable of responding with a JSON object. Please note that as this module is a server, it will remain active, waiting for requests unless a DELETE request is received or the server encounters an error.

PORT

The server connection port number, set to 8080 by default.

Stop on Error

If enabled, the server automatically stops when it encounters an error or exception.

Options Object

In the script, you can directly access the built-in 'options' object. Each key in the 'options' object represents the name of a .txt file in the workspace (excluding the file extension), and each value is the text content of that file. All these files are automatically loaded when the script is executed, saving the need to manually read them within the script:

//Read the text of 'price.txt' in the workspace
let price = options['price']
console.log('price:', price)

Input Object

The input object represents the HTTP request that is currently being processed and includes properties such as 'method', 'path', 'query', and 'body'. For example, when the request is 'GET /orders?date=2023-08-26', the input object received will be as follows:

// Input object of the request 'GET /orders?date=2023-08-26'
{
  method: 'GET',
  path: '/orders',
  query: {
    date: '2023-08-26'
  },
  body: {
  }
}

The requester can use methods like POST and PUT and include JSON data in the request body. In this case, 'input.body' will contain the parsed result of the JSON data provided by the requester.

Output Object

The output object represents the JSON data object that you want to return to the requester and can contain content that complies with the JSON object format:

// Fill in the output object:
output.timestamp = moment().format('YYYY/MM/DD HH:mm:ss')
output.orders = [
  {
    orderId: 1234,
    items: ['coffee', 'milk']
  }
}

The module will automatically repackage the content of the 'output' into the HTTP response and send it completely.

API

Synchronous functions such as api.files(), api.read(filename, encoding='utf8'), api.write(filename, text), and api.remove(filename) are built-in functions that can be used directly. Additionally, an asynchronous function, api.run(uuid), is provided to execute independent skills. Please note that a new api.run() function should only be called after the previous api.run has finished, or it will return an exception.

To store data persistently, the synchronous function api.datastore() will return an object. All data within this object will be automatically written to a datastore.json file in the workspace when the server stops and will be automatically loaded on the next startup.

// Get the datastore object, the server will automatically load it from datastore.json in the workspace
let datastore = api.datastore()
datastore.reqNum += 1

--

We are dedicated to improving our content. Please let us know if you come across any errors, including spelling, grammar, or other mistakes, as your feedback is valuable to us! 🤖️⚡️

Last updated