Building an app like Uber using React-Native
Part 2: Flow for creating REST api
We will start with user api as an example to see how to create our api’s. The flow for driver, trip and cab api’s is similar to the user api and can be taken from github.
Every module will have the following files.
- app.js: This is main file and needed to start the node server
- /routes/route.js: This file will receive all incoming requests and route to respective files
- /routes/user.js : This will act as the servlet/controller to receive the request and return respective response
- /dao/user.js: This will do the task of persisting data into database or fetching data from database
- /util/util.js: This will consist of functions having common code
So let’s look at each file described above
Our app.js looks like
Our user.js file used to receive the requests and return response looks like this.
Our user.js responsible for saving and fetching data is
As you can see we are using ‘diskdb’ an in-memory file database to store our data. The datbase name is saved as emp_user in this case
Once the above changes are done the server can be started using ‘npm start’ from command line.
The server will start running on “localhost:5000”
The source code in github consists of all api’s. These api’s can be tested either from browser or Postman tool
This is a simple server code for our React Native app. This NodeJS code does not cover actual database, will not be able to scale. There is no security included.
In the next part lets look into how to deploy our NodeJS server