Service
With Netuno you can create Web Services and build any kind of complex APIs REST.
Services (Web Services) are used to perform online data integration, where one web system can exchange information with another.
It also provide data to the frontend (web pages) asynchronously, allowing the frontend to load the visual part of the web page faster and then get the dynamic information through integration with services (Web Services).
As will not wait for the whole page to load instead the server will quickly send the structure of the page that will be presented and processed dynamic data gradually, delivering the best User Experience.
The Editor
To create a service on Netuno you will need a code editor.
Is recommended Visual Studio Code as is a freeware source-code editor, fast, quite simple and intuitive.
However, if you choose any other code editor it will work in a similar way.
How to Open Demo Application
In Visual Studio Code by clicking on the left on the first file where you have the option Open Folder.
Browse the file system to the folder where you have Netuno installed.
Inside the Netuno root folder find the folder:
📂 /apps/demo
And when then inside the folder demo click on Open.
Note that the folder and file structure of the application demo has been loaded on the left-hand side.
Creating a Service
The services are processed by the server, so to create a service you must expand the folders and go to the folder:
📂 server/services
Now just right-click on the 📂 services
folder and choose the New File option.
The name of the new file should be 📂 workers.js
, we will create a service with JavaScript.
All you need to do now is to Code our service, which will be as below:
/**
*** Formats the past object in JSON and performs its 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);
The _db.query
receives a string with a database query and performs its execution by transforming the obtained data into a list of objects of type key/value, i.e. column/data.
The _out.json
performs the output (data output) for the browser type ContentType: application/json
and formats the past object to be structured as an object JSON.
Run the Service
To run the service just open in the browser the address of the service that was created which will be:
In this case we recommend the Firefox browser because it allows a better visualization of the JSON object.