Good day.
Not so long ago, it became necessary to implement communication with the server API via
REST requests in android applications. Implementing the software part on android was not a big deal, due to the availability of a convenient and simple library
Retrofit . However, writing a couple of GET / POST requests to existing open APIs (for example, Github and other standard Retrofit examples) made it necessary to start testing the logic of the application. In this case, of course, I would like to have my own server, containing its own data models and having interrelations between the models, as well as different levels of access to specific data models. In this article I would like to tell you how to create a local server in a few small steps, add the necessary models, configure the relationships between them and provide remote access to this server.
I would just like to clarify in order to avoid misunderstandings on the part of the readers:
I, as well as those for whom this article is intended, am a newcomer to the server-side implementation, who by the will of circumstances has fallen into a situation in which I was forced for myself, to raise api as quickly as possible for testing android applications. All the information presented in the article bit by bit can be found in the open spaces of google and youtube and finding it and assembling it into a single whole will not be difficult if you know what to look for. However, this takes time to make decisions about technological implementation, as well as to search for information for each specific decision.
1. NodeJS and Loopback
The first thing that needs to be clarified right away is that the server will be implemented using the Node.js framework
Loopback . First, install Node.js yourself. The latest version of Node.js is located on the
nodejs.org/en/download site, download and install it.
After that, run the command line and enter the following command and wait for the end of the process:
')
npm install -g loopback-cli
2. Creating an application
To create a new application (your server) for the
Loopback framework, go to the directory where your server will be located, enter the
lb command and answer a number of questions about the application, including:
- application name (in my case, test_server)
- the name of the directory for the project (leave empty, then a folder with the name of the project will be created in this directory)
- version of LoopBack (choose the current version)
- type of application (formally speaking - application template. Choose api-server)

After completing the process of creating an application, go to the created folder with the application files and try to start it with the following command:
node .
The application runs on the local address:
localhost: 3000 / explorer . At the same time, the application already has a User model and a number of REST functions.

Formally speaking, your server is ready and successfully processes local requests. You can check the availability and operation of the server using the
Postman application or your android application.
3. Models and interconnections
Next, you need to create models with data and relationships between them. Consider a simple example of a model and relationships: Imagine that our application provides feedback on movies. You enter the name of the film and should receive all the reviews for this particular film. Thus, in the database you have, in the most primitive case, two models should be stored:
Movie (has the fields: name, year) and
Review (autor, description). The relationship between the models is the following; one film can have many reviews.
Thus, the REST-request for movies will have the following link
localhost: 3000 / api / Movies , and the list of reviews will be
localhost: 3000 / api / Movies / {id} / ReviewsCreate two of these models on the server:
lb model
Answer the following questions about the model:
- model name (for example, Movie)
- data source for connection (choose db (memory))
- base class of the model (choose PersistedModel)
- show model using REST API (Yes)
- custom form (left blank)
- general model or only server (choose common)
Now the model has been created and we need to add fields to it (for Movie, for example, name and year):
- property name (for example, name)
- property type (for example, string)
- is required (Yes)
- show model using REST API (Yes)
- default value (left blank)

After adding all the properties, when you see the suggestion to add one more, just press "Enter". Also add the second model.
Now you need to configure the relationship between them. We write a team and answer the questions:
lb relation
- select a model to create a relationship (for example, Movie)
- type of connection (choose has many (one to many), since one movie has many reviews)
- choose a model for the relationship (in our case, Review)
- connection name (any, I will write the reviews)
- user key (left blank)
- intermediate model (No)
- allow nesting of connections (No)
- disconnect communication from the following connected objects (No)

Everything. Now we start the server with the same command as before and watch
localhost: 3000 / explorer . We see that we have our models and we can see the relationship between them through
id .

4. Remote access to the server
Now access to the server is limited to the home network, which is not very convenient when testing from the phone. Let's try to make your server remote. To do this, you need to download
ngrok , unpack to any convenient place and run. This program is designed to create a tunnel for your
localhost: 3000 in order to create remote access for it using the generated link. Enter the following command in ngrok:
npm install ngrok -g ngrok http 3000

We see that the program has created a tunnel and now your server is available at the link provided. Please note that each time you restart your PC, the link in ngrok will change.
Conclusion
Above, a rather rude and dry description of the process of creating the simplest NodeJs server for testing your android application was presented. Naturally, there are a lot of nuances associated with even those 4 steps that I described.
In addition to what I have already described, with just one command you can change the level of access to api and organize user authentication. If anyone is interested - ask questions in the comments - I will answer. The framework itself has sufficiently detailed
documentation , including starting chapters translated into Russian (albeit for version 2.0 with a different set of commands)
Yes, primitive, yes, somewhere technically stupid, somewhere too simple, but for a person who is not involved in server technologies and needs a quick solution to test his main tasks, this solution is as simple and fast as possible.