Skip to main content

Service

With Netuno, you can create Web Services and build complex REST APIs for data integration between systems.

What is a Web Service?

A Web Service allows systems to exchange information online in a standardized way. In Netuno, every file within server/services/ automatically becomes an endpoint accessible via HTTP.

Why use Web Services?

Using asynchronous services offers clear benefits for the user experience:

  • Faster loading: the server delivers the page's visual structure immediately, without waiting for all data to be processed.
  • On-demand dynamic data: the frontend requests only the necessary data, exactly when needed.
  • System integration: a service created in Netuno can be consumed by any external application (mobile, another web system, etc.).
  • Better user experience: the user does not need to wait for the page to finish processing before seeing something on the screen.

Prerequisites

Before starting, ensure that:

  • Netuno is installed and running locally.
  • The demo application has been created as described in Create Application.
  • There is data registered in Tasks linked to Records (necessary to ensure the response is not empty).

Configure the Code Editor

To create and edit services in Netuno, use a code editor of your choice. We recommend Visual Studio Code because it is free, lightweight, and highly intuitive, but any editor compatible with JavaScript, Python, or Groovy will work fine.

Opening the Demo Application

  1. Open Visual Studio Code.
  2. Click the Files icon in the left sidebar.
  3. Select Open Folder (File > Open Folder).
  4. Navigate to the Netuno installation folder.
  5. Select the directory: apps/demo
  6. Click Open.

The folder structure for the demo application will load in the left panel.

Creating the Service

Services are processed on the server side. To create a new service:

  1. In the file panel, expand the structure until you find the server/services/ folder.
  2. Right-click the services folder.
  3. Select New File.
  4. Name the file according to the language you wish to use:
  • server/services/workers.js
server/services/workers.js
/**
*** Transforms query data into JSON output.
*/
const dbRecords = _db.query(`
SELECT DISTINCT
worker.name, SUM(DATEDIFF(HOUR, record.start, record.end)) AS total
FROM worker INNER JOIN record
ON worker.id = record.worker_id
WHERE worker.active = true AND record.active = true
GROUP BY worker.name
ORDER BY total ASC
`);

const list = _val.list();

for (const dbRecord of dbRecords) {
list.add(
_val.map()
.set("name", dbRecord.getString("name"))
.set("total", dbRecord.getInt("total"))
);
}

_out.json(list);

Implement the service by adding the code below to the created file. This service queries the database and returns a list of workers with their total recorded hours in JSON format.

Understanding the Code

Object/MethodDescription
_db.query()Executes an SQL query and returns a list of objects in key-value format (column → data).
_val.list()Creates a mutable list to accumulate the results.
_val.map()Creates a mapping object (key-value) for each result row.
_out.json()Serializes the object to JSON and sends the response to the HTTP request.
Required Data

Ensure there is data registered in Tasks and linked to Records. Otherwise, the endpoint will return an empty list [].

Running and Testing the Service

With Netuno running, access the endpoint in your browser:

Tip for viewing JSON

We recommend Firefox for testing endpoints directly in the browser, as it formats and displays the JSON response in a readable way with syntax highlighting. Alternatively, use extensions like JSON Viewer in Chrome or tools like Postman for more advanced testing.

The expected output will be JSON similar to this:

[
{ "name": "Ana Silva", "total": 12 },
{ "name": "Carlos Mendes", "total": 28 },
{ "name": "João Costa", "total": 45 }
]

Next Steps

Now that your service is working, you can:

  • 📄 Interface & Design — integrate this service into the application's frontend.
  • 📚 Service Library — explore the _db, _val, _out, and other objects available in Netuno.
  • 🔗 Add authentication and access control to your service.