storing info as a JSON file on a server - javascript

So I have information i need to save. I am using a JavaScript object then I store the info in a JSON file.
But I want to put my project on a server and run it. but currently I am using fs to read and write data.
how would i do that on a server

You can have the JSON file in the server and read/write to it the same way you are doing locally, it makes no difference.
That said, I'd recommend using something like http://npmjs.com/package/json-server to make it work like an actual server API.

Related

Is it possible to get JSON from a URL and save it as json file with a custom name inside a specified path/folder through the front end?

My issue is simple on paper - I have a React Native project and I'm trying to make a script that will run on build and fetch JSON from a URL, then save it as JSON file with a custom name inside a specified path/folder. For example, I want to access the JSON at www.example.com and save it as a JSON file in project/src/assets/locales/en.json
The problem is that when I google how to fetch and save JSON, most of the results are related to Node.js and I don't think I can use them. Is what I'm trying to do possible?
Since you've said you're doing this in a git push hook, you can use any of several things for this:
You can indeed use Node.js, by having the hook run it via node, and then using Node.js's http client (to read it) and fs module (to write it to a file).
You can use a shell script, perhaps using with curl or wget (on platforms where those are available).
Since you're already writing JavaScript code for the app itself, Node.js is a reasonable choice.

How to read file client-side without using a file input?

I'm struggling to know how to read a file (client-side) in JS without using a file input, as I only need to read a local file in my project.
What I want to achieve is to get the content from a .less file in order to put it into a parser of some kind (I need to access its content in a javascript file).
I cannot use "fs" as I'm not using node, but I don't seem to be able to use the FileReader API either as it needs a Blob object which would be obtained by the file input.
Does someone have an idea ?
client side doesn't have access to manage file system, you can read file with input but cannot write or delete from client side. it is only possible with a backend application written in node js or java, from their you can expose those services and call it from client side through http requests.

Discord.js create JSON file with server information

Using discord.json, is there any way I can create a json file containing all information for a specific server? (Roles, channels, etc.)
No, I’m not asking for code. I’m asking whether or not this would be possible and if there’s a function for it.
You could use the core module fs to make files (or a json file for your need)

What is the best way to read/write data with javascript?

I want a way to really simply read/write to a json file (as my storage) online. I want to be able to host an app and make axios requests to this to read the data, update it locally and then push the data to the online resource. is there any simple way to do this?
I've used firebase in the passed but this is too long for me to set up now. also http://myjson.com/ only seems to let me do reads. there must be a quick way to host data in a json format that allows reads and writes
I also don't want to use localstorage because other people will be accessing the app
I think it's unfortunate your question is getting down-voted - but that's because the premise of your question is off. Reading/writing a single JSON file is expensive (e.g. timewise) and would be a naive approach (that's okay, we've all been there). What you want is a database. I would suggest starting off with a simple database such as mlab. But even before that, I would suggest you read this article - it gives a good overview of what a CRUD app is.
But if you really want to use a JSON file (again, I would highly NOT recommend this). Then:
You'll need to setup a server
Expose an API endpoint (where you handle GET/POST etc requests) for that server, so clients/people can communicate with said server
Have the server talk to some kind of store (e.g. https://aws.amazon.com/s3/) where the file would live
steps 1. and 2. you'll eventually need to do for any server. But step 3. is why I would NOT recommend using a JSON file. That's because it's very expensive (as noted before) to get the file, change the file, and then put that file back in the store. At that point, you might as well use a database.
Good luck! :)

WebSQL Database Upload

I have a WebSQL database, and am trying to connect to a PHP script, and dump that database into another one on the server.
How do I format the javascript / jquery in order to do that correctly?
I guess I am asking, how do I dump all of my data in my tables into my PHP script, so that it can update and insert the data into another database on the server?
I have no clue where to even begin with this.
The terminology in this question is a little confusing. If you're trying to copy the database from one location to another on the same server, you would only use PHP at most. This could also be done with just your sql administration.
If you are attempting to copy the database to a "Web SQL" database on a client (not a server), I've found a round-a-bout sort of way to accomplish this. I say round-a-bout because in my case I had to copy the database from the server to local client files (aka cross domain). In short:
1) Use PHP to create a json_encode version of your database from the server, using fwrite to create a .json file on the server
2) Using a php proxy to deliver the json data, make a jquery ajax call to the proxy, for the .json file you created
3) Use javascript to store the JSON data into the client's "web sql" database.

Categories

Resources