5 simple steps to creating a server for testing android REST requests

  • Tutorial
Good afternoon.

Not so long ago, there was a need to implement in an android application communication with the server API through REST requests . Implementing the software part on android was not difficult, due to the presence of a convenient and simple Retrofit library. However, having written a couple of GET / POST requests to existing open APIs (for example, Github and other standard Retrofit examples), it became necessary to start testing the application logic. In this case, of course, I would like to have my own server, which contains its data models and has interconnections between models, as well as various 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 immediately like to clarify in order to avoid misunderstanding on the part of readers:
I, as well as those for whom this article is intended, am a beginner in the implementation of the server part, who, by chance, got into a situation in which I was forced to raise the api as quickly as possible for myself to test the android application. All the information presented in the article bit by bit can be found on the open spaces of google and youtube and to find it and assemble it into a single whole will not be difficult if you know what to look for. However, this takes time to make decisions on technological implementation, as well as to search for information for each specific decision.

1. NodeJS and Loopback


The first thing you need to immediately clarify the server will be implemented using the Node.js framework- Loopback . First, install Node.js. itself. The latest version of Node.js is on the site nodejs.org/en/download , download it 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. Create an application


To create a new application (your server) for the Loopback framework, go to the directory where your server will be located on the command line, enter the lb command and answer a number of questions about the application, including:

  • application name (in my case, test_server)
  • directory name for the project (leave blank, then a folder with the project name will be created in this directory)
  • LoopBack version (select the current version)
  • type of application (formally speaking - an application template. Choose api-server)



After completing the process of creating the application, go to the created folder with the application files and try to run it with the following command:

node .

The application runs at the local address: localhost: 3000 / explorer . Moreover, 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 relationships


Next, you need to create models with data and the relationships between them. Consider a simple example of a model and relationships: imagine that our application provides feedback on films. You enter the name of the film and should receive all the reviews for this particular film. Thus, in the most primitive case, two models should be stored in the database: Movie (has fields: name, year) and Review (autor, description). The relationship between the models is as follows, one film can have many reviews.

Thus, the REST request to the films will have the localhost: 3000 / api / Movies link , and the localhost: 3000 / api / Movies / {id} / Reviews to the review list

Create these two models on the server:

lb model

We will answer the following questions about the model:

  • model name (e.g. Movie)
  • data source for connection (select db (memory))
  • base class of the model (select PersistedModel)
  • show model using REST API (Yes)
  • custom plural form (leave blank)
  • general model or server only (select common)

Now the model is created and you need to add fields to it (for Movie, for example, name and year):

  • property name (e.g. name)
  • property type (e.g. string)
  • is mandatory (Yes)
  • show model using REST API (Yes)
  • default value (leave empty)



After adding all the properties, seeing the offer 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 (e.g. 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)
  • communication name (any, I will write reviews)
  • user key (leave blank)
  • intermediate model (No)
  • Allow Nesting (No)
  • disconnect communication from the following connected objects (No)



All. Now we start the server with the same command as before and look at 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, download ngrok , unpack it at any convenient place and launch it. This program is designed to create a tunnel for your localhost: 3000 , 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 provided link. Keep in mind that every time you restart the PC, the link in ngrok will change.

Conclusion


It was presented above a rather rough and dry description of the process of creating a simple NodeJs server for testing your android application. Naturally, there are many nuances associated even with 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 the api and organize user authentication. If anyone is interested - ask questions in the comments - I will answer. The framework itself has fairly detailed documentation , including the start chapters translated into Russian (albeit for version 2.0 with a different set of commands)

Yes, it’s primitive, somewhere technically stupid, somewhere too simple, but for a person who is not involved in server technologies and needs a quick solution to test their main tasks, this solution is as simple and fast as possible.

Also popular now: