Just Patrick
Posted on July 27, 2020
Its Finally here guys, as promised. Today we are going to setup your phone for some back end development. I'll walk you through all the tools you need to continue coding your apis on your Phone, on your couch ππ. We are going to hook up a rest api with some CRUD functionalities to our MongoDB service RUNNING ON OUR PHONEππ.
Replacements
1. Terminal/Linux environment
Its none other than the Legendary Termux app. This app is a terminal emulator that will provide a sort of Linux enviroment/shell(correct me if am wrong though) where we can install NodeJs,Git and Vim. Of course termux has A LOT more capabilities and functions but for today we will just use it for some JS development. I do encourage you dive in more deeper into termux as it is just freaking amazing and has a pretty nice community.
2. Postman
In order to test our routes we will be using an app called REST Api Client which you can download here.
3. MongoDb Compass
For our mongodb service we will be using an app called Dory Mongo which you can also directly download using this link. The developer pulled it down from play store but you can get from apkpure(the website). You can ofcourse just use the connection string from your remote database running on Atlas.
OK, if your done with downloading the apps we can start.
Setting Up Termux
So when you first launch termux you'll be greeted by this screen.
You can treat these as the bare environment you get after installing Linux. Let's first update termux by running this command apt update && apt upgrade
.
As I said we need NodeJs,Git and Vim(optional, if you hate it, nano is preinstalled).
Just run the commands below
pkg install git
pkg install nodejs
***NB: This will install will install the Latest release of Node with for my case is v 14.0.0*
pkg install vim
Lets create a shared folder where we will be storing all our project files. I recommend this as the termux files and folders you create in the root directory will not be visible in your Default File Manager.
First give termux access to your storage by running this command
termux-setup-storage
Then lets create our folder in the shared Directory.
cd storage/shared && mkdir Termux
We can now access this folder from Termux and from our File Manager
Initializing our Project
You can just go about this using your normal workflow like mkdir Backend && cd Backend && npm init -y
and so on. But to shorten this tutorial i'll just pull some boiler plate code for a Node Mongoose CRUD api from github.
git clone https://github.com/fedosejev/restful-api-express-mongoose.git
cd restful-api-express-mongoose
npm install
This is how the project looks like
To start the server we can run the script node app.js
but not yet. We need a mongodb connection silly π
.
Starting a MongoDB Service
Again this is not necessary if you have a remote mongo service running in the ββ. You can just replace the IP and Database name in the connection string with your own.
But lets focus on offline for when your at your grandma's and and there's no network π.
- So open the Dory Mongo app and click on the play button to start the service.
- Replace the IP address in your connection string with the one nemo, sorry Dory, is running on and replace the database name part with...you guessed it, A database name of your liking.
Ok, so now we have setup a mongodb service and connected to it. You can now finally start your server(node app.js
) πππππππ
Our server is finally running on port 8080 but now we have to test our api to see if we forgot any semicolon in our code(πJava guys)
Testing our routes
- Open the REST Api Client App.
- Switch to http on the left of the input field
- And Finally enter the address as
127.0.0.1:8080/api/items/
- Select POST, enter your Json Data and click Send
You will the be shown a response from the server
NB: Go to the Models folder of the project and see what type of data is expected
You can now run other requests, GET,DELETE,PUT and so on.
Phew we are finally done. That was simple right. "Hey, what was Vim for Bro?". Sorry I almost forgot. Its of course for editing your files from termux. You can 'cd' into your project and run vim .
then select the file you want to edit. or just use nano. or even better use Spck editor.
Combine this article with my previous one about spck editor and wallah, You are doing Fullstack Web Developmet on Your Phone
Have a great day.
Posted on July 27, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.