json-server used with function calls instead of API request? - javascript

My purpose is to persit and manipulate my application data locally in a json file without using a database.
Is it possible to use json-server in a non API mode, that is, to manipulate a json file with function calls (GET, PUT, DELETE, ..) instead of API Request ?
If not, can you suggest an javascript library that has the same features as json-server ?

Related

In Marklogic, I have a custom JavaScript function. How do I call it through REST API? What is the process of calling it from CURL?

I have a few documents in my database and I've designed search functions for them. So I want to use curl in cmd and call the function so that I can run the searches and print the results in cmd only.
For example, I have a function where I'm passing the "userID" and I want all relevant documents. So I need to pass that value in the code or can I specify this(params) in curl.
If not curl, what other REST API options do we have to call custom JavaScript functions?
One approach is to create a REST API server and use either the invoke or resource service extension endpoints to invoke your script.
For information about standing up the REST API, see:
http://docs.marklogic.com/guide/rest-dev/intro#id_97899
For information about invoking code, see:
http://docs.marklogic.com/guide/rest-dev/extensions#id_72813
For information about a resource service extension (which requires that your code conform to conventions), see:
http://docs.marklogic.com/guide/rest-dev/extensions#id_21018
Hoping that helps,

trying to update JSON file in react

I have a JSON file 'DealerList.json' and I can access it from my app by importing it using
import DealerList from './json/DealerList'
let tasks = DealerList;
The tasks here contain the JSON and I can easily access it.
Is there any way by which I can update the JSON back like if I update the values OR if I delete the values. I have also tried require("fs") but that too is throwing error.
The JSON file is present in a folder called json in the React App.
No, and this is a good thing. You do not want any web browser to be able to come along and rewrite files on your HTTP server.
Write a web service which will read and update the data based on HTTP requests (with appropriate authentication/authorization to stop undesirables from changing the data). Then interact with it via XMLHttpRequest / fetch / an abstraction library wrapping one of them (like axios).
(Or use localStorage if you want to store the data on a per-browser basis rather than shared between visitors).

Write JSON data from front-end to back-end in nodejs

I have ButtonClick.js and TakeData.js files. I defined my json data in TakeData.js as below
var dataObj = {};
var locationsObj = "locations";
dataObj[locationsObj] = {};
dataObj[locationsObj].source = [];
dataObj[locationsObj].target = [];
When I click the button in ButtonClick.js and the code snippet as below
button()
.container(g2)
.text(text2)
.count(1)
.cb(function()
{
console.log(dataObj[locationsObj].source[0]);
console.log(dataObj[locationsObj].source[1]);
console.log(dataObj[locationsObj].target[0]);
console.log(dataObj[locationsObj].target[1]);
console.log("SaveFile");
})();
I want to send json data into the nodejs file writing function as the following.
fs.writeFile('sample.txt', [need to insert my JSON], function(err) {
if(err) return console.log(err);
console.log('Hello JSON > sample.txt');
});
How can I do that? Is there another effective way?
Your backend needs to have an HTTP server listening on some port that is accessible to your frontend. Then your frontend needs to make an AJAX request to your backend (most likely a POST request) and send the required data in the request body.
Now, your backend needs to handle the request, get the data and write it to the file or do whatever you want with that.
Things to keep in mind:
use body-parser if you're using Express
remember about CORS
It's easier if you use a higher level framework on the backend like Express, Hapi, Restify, LoopBack etc. instead of the low level http module in Node.
It's also easier if you use a framework on the frontend like jQuery, Angular, React, Aurelia, Ember etc.
The first step is to set up a RESTful POST operation (an HTTP POST) on your server. This is the typical service mechanism for what is sometimes called an AJAX call. Once you have the server set up to receive a string via the POST, you can serialize your objects on the client side and deserialize (reconstitute) the objects on the server side.
To serialize, you can use stringify on the client side. A simple web search for "stringify" will show you different ways to use stringify in a browser-independent, backward compatible way.
stringify(obj)
On the server side, node.js has a global JSON object. Use parse to reconstitute an object or objects from the string. Other major languages now have similar parser methods. 1
JSON.parse(strJSON)
To get started, you can test the mechanism with just one simple object. Then you can aggregate the objects in a JSON array or associative array and send them all at once in a single POST.
[1] There are frameworks that encapsulate this process, but it may be of value to you to NOT initially use them so you get a clear picture of the mechanics of a RESTful POST over the HTTP protocol. It is the key thing that a browser does when you submit an HTML form, and RESTful operations are key communications elements in today's IT world.

UIAHost Request example with json

has someone any ideas, how can i make a request with UI Automation and JS with UIAHost. An example is welcome with POST as method, some data and json as datatyp.
Thank
The easiest way to make an HTTP request via UIAutomation would be to make a cURL request via the shell using performTaskWithPathArgumentsTimeout.
If you are making a POST request with a lot of data, you may want the shell command to read the data from disk (instead of specifying it all on the command line). Our Illuminator project provides a function that lets you write arbitrarily large files to disk from within UIAutomation.

Call JS through cURL

I am trying to call my js file through cURL. That JS file in-turn call's third party API to fetch me the result.
Overview of JS file.
In js file i am creating an object of the 3rd party library file and sending them the relevant data to get the response back. This response need to be captured and returned back when call'd through cURL.
Procedure of calling cURL
The cURL function will be triggered as an API call.
What i have tried so far !
I am storing the response of the library file in a cookie. So that i can use the same. I am able to achieve this through browser. But, when tried with REST client, i get all my <script> function as response also it doesn't call the library file too.
So, how can i call the JS file through cURL OR is there any other alternative way to achieve this. ?
NOTE : It is not a regular browser flow, it has to be a API call.
Please let me know on this !

Categories

Resources