FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour HIX -> Consuming Node.js API
Posts: 476
Joined: Sat Feb 03, 2007 06:36 AM
HIX -> Consuming Node.js API
Posted: Wed Oct 29, 2025 01:03 PM
Testing with HIX to consume a Node.js API to access a MaridaDB/MySQL server:
#xcommand TRY  => BEGIN SEQUENCE WITH {| oErr | Break( oErr ) }
#xcommand CATCH [<!oErr!>] => RECOVER [USING <oErr>] <-oErr->
#xcommand FINALLY => ALWAYS

function main()

  local cHtml := '', oHttp, cUrl, cJson, hParams, cResponse
  
  hParams := {=>}
  hParams["action"] := "ping"
  cJson := hb_jsonEncode( hParams )

  cUrl := "http://192.168.0.50:3002/api/ping"
  oHttp := TApiConnNodeJs()
  oHttp:Open( "GET", cUrl, .F. )
  
  oHttp:SetRequestHeader( "Content-Type", "application/json" )
  oHttp:SetRequestHeader( "Accept", "application/json" )
  oHttp:SetRequestHeader( "Cache-Control", "no-cache" )

  oHttp:SetRequestHeader( "Connection", "Keep-Alive" )
  oHttp:Send( cJson )  
  cResponse := oHttp:responseText

  cHtml += '<h2>Information test ApiNodeJs</h2><hr>'
  cHtml += '<small>Processed at ' + dtoc(date()) + ' ' + time() + '</small>'
  cHtml += '<hr>'
  cHtml += 'responseText: ' +cResponse
 
return cHtml 

//-------------------------------------------------------------------------------------------------------------------
function TApiConnNodeJs()
LOCAL lHttp, oHttp

lHttp := .f.

TRY
  oHttp := CreateObject( "MSXML2.ServerXMLHTTP.6.0" )
  lHttp := .t.
CATCH
  lHttp := .f.
END

IF !lHttp
	TRY
	  oHttp := CreateObject( "MSXML2.ServerXMLHTTP" )
	  lHttp := .t.
	CATCH
	  oHttp := CreateObject( "MSXML2.XMLHTTP" )
	  lHttp := .t.
	END
END

RETURN oHttp
And here's the result:


The Node.js API is installed on a local server running Linux Ubuntu 24.04.

Great work with HIX, Carles.

Best regards.

Carlos.

Continue the discussion