Routes
How to create a Route
A Route is basically a backend REST API, where you write code for some DB Operation or anything else. You make request to this route to get some data or execute any other logic on the server.
In order to create a route, fill the following details in the form : -
Name - name of the route.
Path – endpoint URL of the route.
Active – Check / Uncheck if ready / not ready to used.
Route Type: - MS – route type is microservice. SITE – for serving static content. API – for serving data from database. CORE – core route is for system specific operations.
Method: - POST – to post some data. GET – to get some data.
Script – Code goes here.
Click on ‘create’ button. That’s it, you have created a Route.
MS / API Router :-
MSV2 / API Router :-
How to use API Routes
In this guide, we'll walk through the process of creating an APIs like you do in your local nodejs setup without the need for deployment.
Our example will use OB API exposed by the platform for you. For every native API of nodejs you will have to add ob.
as a prefix.
And thats it you can use native nodejs functions like you do here in your local setup.
Inserting Data with ob.db
Here, we create a new user, save it to the database using the ob.db.create
method provided by ob.db, and log the saved user to the console.
Querying Data with ob.db
The ob.db.find
method queries the database for users with the name 'John Doe', and the result is logged to the console.
Updating Data with ob.db
In this snippet, we use ob.db.update
to update the email address of the user named 'John Doe' and log the result.
Deleting Data with ob.db
Here, we employ ob.db.delete
to remove the user with the name 'John Doe' from the database, and a message is logged upon successful deletion.
Reading and Writing Files in Node.js In Node.js, working with files is a common task. Whether you need to read data from a file or save information to a file, Node.js provides built-in modules that make file I/O operations straightforward.
Reading Files with Node.js
Let's start by looking at how to read the contents of a file. Node.js provides the fs
(File System) module for file-related operations. The following code snippet demonstrates how to read a file asynchronously using the fs.readFile
function:
In this code snippet:
fs
: Import thefs
module.filePath
: Specify the path to the file you want to read.fs.readFile
: Read the file asynchronously, providing the file path, encoding ('utf8' for text files), and a callback function to handle the result.The callback function receives an error and the file content. If an error occurs, it's logged; otherwise, the content is printed to the console.
Writing Files with Node.js
Now, let's explore how to write data to a file. The fs.writeFile
function allows you to write data to a file asynchronously. Here's a code snippet demonstrating file writing:
Explanation of the code:
fs.writeFile
: Write to the file asynchronously, providing the file path, data to be written, encoding, and a callback function to handle errors.The callback function logs an error if one occurs; otherwise, it prints a success message.
These are basic examples of reading and writing files in Node.js. Depending on your use case, you may need to explore additional options and methods provided by the fs
module.
Last updated