How to create an Android application using Flutter + NodeJS – Part 3

Tram Ho

Hello everyone, the content of today’s tutorial will be creating a server and providing an API to prepare for the app. The project is quite long and touches a lot of knowledge, you need to prepare the following basic knowledge:

  • GIT
  • JavaScript, ES6, mongoose
  • MongoDb, you can see more tutorial
  • Express NodeJs, you can see more tutorial

Requirements for setting up the necessary environment:

  • IDE: intellij (you can use Visual Studio Code, Sublime Text ….)
  • NodeJs here
  • Mongodb here

The goal of this article will be to provide APIs for the app, including:

  • get list category:

BASE_URL / category

https://gentle-fjord-54250.herokuapp.com/category

  • get list recipe by category:

BASE_URL / category / {category_id}

https://gentle-fjord-54250.herokuapp.com/category/5d67293df35b984e744868e0

  • get recipe detail by id:

BASE_URL / recipe / {recipe_id}

https://gentle-fjord-54250.herokuapp.com/recipe/5d6f2d0c89771377812fbad2

  • search recipe by name:

BASE_URL / recipe? Q = {name}

https://gentle-fjord-54250.herokuapp.com/recipe?q=mat+water

BASE_URL when doing locally will be: localhost:8081 and deploy on the host will be gentle-fjord-54250.herokuapp.com (depending on the domain you register)

First, you look at the following model to see clearly the relationship between the app, server, database like.

  • Database is the place to store information for the app to display, which is the crawl
  • The server will be the place to receive requests from the app, then handle the request (analyze reques, query the database to get data …), and finally return the response as an API for the app.
  • The App will get information from the API to display on the interface.

The basic thread of execution will be the same. First step I will work with Database.

Database structure

Here I use the NoSQL form db as Mongodb. You need to install mongodb before doing the next steps

First I will create a database named cooking_db

use cooking_db

Next is to create a document as category

db.createCollection (“category”)

In db, there is only 1 collection with the following structure

All crawl data that I have synthesized into 1 file, you can download here . After downloading, you will import it into db with the following syntax:

mongoimport –db dbName –collection collectionName –file fileName.json –jsonArray

Inside:

  • dbName is cooking_db
  • collectionName is category
  • fileName.json is category.json

Remember to open the termial to execute this command in the same folder containing the json file, otherwise the path is wrong and won’t run.

After successful import, you can check if the collection has data, with the following command:

db.category.find ()

If the result shows the same data as in the category.json file, you have successfully imported it. If you do not see anything, the import process has a problem, you carefully check each step and then import again, if still not, please comment below the article, I will see it for you. You have now completed the database structure.

And for the server to be able to connect to the database, you must start mongodb during the server run. You can see how to start here

Server p1: initialize the project

First, you need to create a project to work, here I use intellij and create a project js. Because of this intellij guy, he helped create some basic configuration files for the project. You can create a blank project and add these files yourself, no problem.

The project structure will consist of the following basic packages and files:

Inside:

  • controller: is the main place handling requests
  • model: is where the classes are defined
  • router: is the place to receive requests from the app, then navigate to the corresponding controller to handle the request
  • public: is the location of rerouce such as photos, videos
  • gitignore configuration file for git, you can skip or download here
  • Procfile configuration file when server deploy on the system, here I just do it locally, so this part you temporarily skip
  • README.md configuration file for git, you can skip
  • The app.js file starts the server, then connects the database, and other basic configurations for the server
  • The package.json file is a dependency declaration file, used to import modules / libraries that need to be used File structure like this:

Here you only need to care about 2 main items:

“main”: “app.js”,

and

“dependencies”: {“express”: “^ 4.17.1”, “mongoose”: “^ 5.6.12”},

main will specify the file to be run first when running the project, dependencíe as above mentioned that it will contain the required modules / library. I use express and mongoose so I need to install these 2 modules. The installation, you just need a few commands npm is done well, you find this place yourself, very easy. I put the link for instructions here: nmp expres , npm mongoose . Inside:

  • express: is a js framework that helps you create APIs quickly and easily, in accordance with its name
  • mongoose: help you create Schema manipulating database mongodb

OK, the explanation and setup of some basic information for the project is completed, next I will go to the app.js file, this is the file that started everything.

I want you to only need to care about the areas that you circle, other parts can be temporarily ignored. Inside:

  • 1, 2 is the import declaration of module / library
  • 3 is the connection to the database I just created
  • 4 is server initialization

Maybe you do not understand, or understand not yet, please follow me to run the code first. The reassuring you do not understand much at first, just run it for it and then continue to count. Now you can run the project using IDE or use the command to run it

node app.js

The result that prints “Server is running …” “Connection successful” is initializing the server and connecting to the database successfully. If there are errors, please check parts 1, 2 to see if the import is correct, part 3, see if mongodb is being started or not, the path name to the db is correct, part 4, see if the syntax is correct.

Server p2: create the model

According to the collection structure that I mentioned above, I will create corresponding models to map with this format. Here I need to create 4 models: category, recipe, component, cook_step, corresponding to a .js file as follows:

category.js

recipe.js

component.js

cook_step.js

Ok, this step is as simple as that. You can refer to your code here

At this point, you are halfway through creating a server. Because this section is quite long, I will divide into 2 articles for you to be more comfortable, the next article I will talk more about how to receive requests from the client, query the database and return the API again. Well there is another problem that deploying to the server can run like the link I have on the top of the article. And now you should practice to do 1/2 already.

See you in the next post.

Share the news now

Source : Viblo