1

NodeJS Async await

Тема: NodeJS Async await

Hello guys in this post I gone show you how to use NodeJS without Wialon JS sdk, my principal reason for use wialon in that way is we have mutiple session in the same server and we develop our apps in MVC (Model,View,Controller) and in this develop model the code is hierarchized and separated into different folders and fields and use sdk in native form it's so complicated at less for me.

Another reason is the code based in callbacks is so dificult to maintain and scale.

For this reasons I created my own tiny wialon request handler to nodejs, this tool is very simple but works fine for my, I'm a habitual user of remote api for me is not a problem generate the params value manually.

i hope this tiny-tool will be useful for someone.

Using this tool

let request_generator = require('./request_generator.js');
async function  login(_token){
  let login_response = await request_generator.request_generator('','token/login',{token:_token,operateAs:""});
  console.log(login_response);
}

request_generator.js file

let fetch = require('node-fetch');

 async function request_generator(_sid,_svc,_params,_base_url='https://hst-api.wialon.com/wialon/ajax.html?'){
   try{
     let response = await fetch(_base_url+'sid='+_sid+'&svc='+_svc+'&params='+encodeURI(JSON.stringify(_params)));
     let data = await response.json();
     return data;
   }catch (e){
     console.log(e);
     return {error:'ocurrio un error inesperado'}
   }
 }

 exports.request_generator = request_generator;

Regards.