📜 ⬆️ ⬇️

5 easy steps to create a server for testing android REST requests

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:




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} / Reviews

Create two of these models on the server:

 lb model 

Answer the following questions about the model:


Now the model has been created and we need to add fields to it (for Movie, for example, name and year):




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 




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.

Source: https://habr.com/ru/post/420993/


All Articles