Skip to main content

REST APIs

In order to create your REST APIs just add an apis/ folder to your less/ folder.

mkdir -p less/apis

Let's create a demo API. We will use it to configure our routes.

mkdir less/apis/demo
Create multiple REST APIs

Each folder at the root of less/apis/ will be deployed as a separate API and will have a separate base URL.

Creating API Routes

The API's route paths mirror the relative path for your project files.

Let's create a /hello route in the Demo API.

mkdir less/apis/demo/hello

HTTP Verbs

In order to process requests, create your handler functions inside of the route's folder. The handler name is the HTTP verb plus the file type.

GET /hello

Let's create a GET /hello route that returns "Hello, world." in the response body with a 200 code.

touch less/apis/demo/hello/get.js
less/apis/demo/hello/get.js
exports.process = async (request, response) => {
response.body = 'Hello, world.';
return response;
};

POST /hello

Let's create a POST /hello route. We will accept a name in the body and return "Hello, {name}." in the response body with a 201 code.

touch less/apis/demo/hello/post.js
less/apis/demo/hello/post.js
exports.process = async (request, response) => {
const request_data = JSON.parse(request.body);
const name = request_data.name;
response.body = 'Hello, ' + name + '.';
response.statusCode = 201;
return response;
};
tip

PUT, PATCH, and DELETE requests work the same way. Just add a file with the folder name to the route directory.

API Dynamic Paths

In cases where you would like to have a dynamic path just wrap your folder name in {}.

GET /hello/{name}

Let's create a GET /hello/{name} and say "Hello" to the name provided in the route path:

mkdir less/apis/demo/hello/{name}
touch less/apis/demo/hello/{name}/get.js

Here's how we can access the value of name from the route path:

less/apis/demo/hello/{name}/get.js
exports.process = async (request, response) => {
const { name } = request.params;
response.body = 'Hello, ' + name + '.';
return response;
};
note

We now have a Demo API with 3 routes:

  • GET /hello
  • POST /hello
  • GET /hello/{name}

HTTP Request/Response Properties

Status Code

You can optionally set the response HTTP status code through the statusCode property. The default value is 200.

less/apis/demo/hello/post.js
exports.process = async (request, response) => {
response.statusCode = 204;
return response;
};

Headers

You can access request and response headers through the headers object.

Here's how we can access the Accept header in a request and set the Content-Type in a response:

less/apis/demo/hello/get.js
exports.process = async (request, response) => {
const accept_header = request.headers.Accept;
if (accept_header === 'application/json') {
response.body = { hello: 'world' };
response.headers['Content-Type'] = accept_header;
}
else {
response.body = 'Hello, world.';
}
return response;
};

In this section we'll see how to access data in our HTTP requests.

Body

You can access the request and response body data through the body string.

Here's how we can access the value of name passed as a query param (e.g. GET /hello?name=Djassi):

Request and response bodies should always be in string format.

Convert your data as needed.

less/apis/demo/hello/post.js
exports.process = async (request, response) => {
const request_data = JSON.parse(request.body);
const name = request_data.name;
response.body = 'Hello, ' + name + '.';
return response;
};

Query Parameters

You can access query parameters through the query object in the request.

Here's how we can access the value of name passed as a query param (e.g. GET /hello?name=Djassi):

less/apis/demo/hello/get.js
// GET /hello?name=Djassi
exports.process = async (request, response) => {
const name = request.query.name;
if (name) {
response.body = 'Hello, ' + name + '.';
}
else {
response.body = 'Hello, world.';
}
return response;
};

Middleware

Middleware is a widely utilized concept in web development, serving as an intermediary layer between the client and the server to manage various tasks. These tasks can include authenticating requests, parsing data, logging, and more. LESS offers a way to work with middleware similar to how middleware is handled in a Node.js Express application. In your API route, in addition to passing the main process function, you can also pass an array of middleware functions to your route. This approach allows each middleware to process the request sequentially before it reaches the main handler.

less/apis/demo/hello/get.js
// Middleware function structure
const middlewareFunction = async (request, response, next) => {
// Add your middleware logic here

// Call next() to proceed to the next middleware in the stack,
// or to the route handler if this is the last middleware
next();
};

module.exports = {
middlewares: [middlewareFunction],
process: async (request, response) => {
// Add your route handling logic here

// For example, setting a response and sending the response,
// by default status code is 200
response.body = JSON.stringify({ message: "Success" });

return response;
}
}

Deploy your REST API

npx @chuva.io/less-cli deploy my-less-project

Test your REST API

Once the deployment is complete you will be able to find your Demo API under Resources in the output:

[less-cli] Deployment complete ✅
[less-cli] Resources
[less-cli] - API URLs
[less-cli] - Demo: https://my-less-project-demo.api.eu-0.a83b464c9.less.chuva.cv
[less-cli] 🇨🇻
  • GET /hello
  • POST /hello
  • GET /hello/{name} You can test your routes using curl:
curl [BASE_URL]/hello
curl -X POST [BASE_URL]/hello -d '{"name": "Djassi"}'