HTTP
Last updated
Was this helpful?
Last updated
Was this helpful?
Now that we have data from XDK (see more about it in ) we need to have a way to save it in a database and actually to can use it in some applications, having this in mind, we are going to build a powerful RESTful API using nodeJS, Hapi.js, and mongoDB.
So we are going to start initializing a npm package, and install all dependencies that we'll use...
request
mongoose
Nodemon
hapi.js
Will help us to use http verbs inside our server
Will help us to query our database
Will help us to can hot reload our server just saving
Will help us as the selected framework to build a server with nodeJS
Basically its all we need for our server, after this we are going to create the following model:
Models
xdk_virtualsensors.js
Routes
routes.js
xdk.js
index.js
First of all we need to create our index.js, define our port, initialize plugins, and connect to database.
In Models we are going to define our schema using mongoose tool called "schema", basically is just define what we are going to add to our database, and what kind of parameters this schema is going to have such as type of variable to add, if one variable is required or not, etc.
the schema for xdk_virtualsensors is the following:
after this we need to define our routes in routes.js / xdk.js. Basically we'll put what routes are available and in the server side what to do when some routes is reached. what we are going to build is a CRUD API.
Here is the code that we are going to use for GET data in real time, I already have code for GET data from timestamps, delete data, and create data from XDK firmware code. For easier understanding we are just going to use the part where we add our data to mongoDB and where we ask for the data from frontend.
For use it just copy the following steps in your terminal...
Of course you need to configure your database path, and your own routes if you want, it could works as a boilerplate for your own project, Enjoy it!
GET
http://localhost:3000/xdk/virtualsensors/{name}
Get last XDK data stored in mongoDB using mongoose.
string
Name of XDK (for have a better organization and know who is creating or getting data.) And actually you can make restriction about who is getting data.
POST
http://localhost:3000/xdk/{name}/virtualsensors
Add XDK data to mongoDB in real time.
string
Name of XDK (for have a better organization and know who is creating or getting data.) And actually you can make restriction about who is getting data.
In this case I already have a local database where I am saving my data. Its not difficult to install mongoDB and use it, but if you don't know how to do it look at this link .
In this part is important to see how mongoose works in its documentation because mongoose is the tool that we are going to use for query database, its pretty simple to use it but for further information check it out here --> .
Thats all what we need for using this backend if you want you can download my code that is in my github account --->
Find my source code !