Save a file with nodejs to external server - javascript

So i have set up a basic nodejs server using express and everything works fine. I have a json file in my folder that i want to be able to modify, to add some data or remove some.
What is the best way to do that without the user being able to interact with the actual file? Perhaps a database that i could send my data to?
I am very new to nodejs and javascript so anything that is as simple as possible is the best.

You could create different http routes for setting and fetching data in the json file.
Say, for example:
GET /data would read the json file and respond with the contents of the file.
POST /data could post the contents of the file.
PUT /data/key could be used to modify the contents of one single key in the json.
That being said, this looks like something that you should be using a DB for. If somewhere down the line, you choose to dockerize your app, everytime you restart your server, your JSON file would be reset to the initial config.
To avoid that, a db could be used. Mongo is a good place to start considering your choice of language and nature of your data.
Hope you find this helpful :)

Related

Managing json files on the server with nodejs

Recently, I found that there is called 'LowDB' and it can control the json file with NodeJS.
Actually, I can use MySQL or other databases but I think the App that I developing now is small application so it needs very tiny DB like simple Json.
This Link is connected to lowDB example.
Link
https://github.com/typicode/lowdb
As you can see it controls json file and it includes CRUD(Create, Read, Update, Delete) function. But the data are save in Local storage not in server. So Even I control the json files it will only apply in Local Json file. I want to save it in server.
How can I manage json file with NodeJS? Please give me some keywords or introduce some Node Dependencies.

Modifying Text file in javascript

I need to have data written to a text file in javascript. I want it to write a username and password to the text file and create a new line every time. Here is my code http://pastebin.com/24Tvdemu.
Can anyone help this has had me stumped for ages.
As Javascript in html is a client side language, you will need to send the files to the server, and save there the file. Anyway, you can prompt the user to save the file in their local machine, but it´s not usefull at least you really need that for any reason.
Check this answer Javascript: Create and save file
Some suggestions for this -
If you are trying to write a file on client machine, You can't do this in any cross-browser way. IE does have methods to enable "trusted" applications to use ActiveX objects to read/write file.
If you are trying to save it on your server then simply pass on the text data to your server and execute the file writing code using some server side language.
To store some information on the client side that is considerably small, you can go for cookies.
Using the HTML5 API for Local Storage.
More details : Is it possible to write data to file using only JavaScript?

Read json file in nested folders

I have director structure like this, only $(Root Directory)'s name is known at run time.
other folders and files are generated dynamically.(All files are .json)
my requirement is I need to count no of files and read content of all files using jquery and ajax.
I know if we have some static path like abc/xyz/somefile.json then we can read someFile.json but in my case I need to traverse nested folders.
help
You will not be able to do it without server-side support, either from the web server itself or another server-side process.
If directory listing is enabled on your web server, you can make an ajax request directly to the directory you want and scrape the returned document's content.
Another way would be to setup a web service which allows to query a directory's content. That service would be responsible for querying the file system and return the information to the client in a data-interchange format like JSON.

Open a text file from server on the Client-side using Javascript

No matter how much I look this up all I get is the w3C File API which focuses on local files for the client.
What I'm trying to do is I have a server. I'm trying to use client-side javascript to grab the server hosted text file, a.txt, and display it to the innerDOM of an html page. My server directory look like this:
index.html
read.js
text files
a.txt
All I want to have happen is for, on the client side, the javascript read.js running in the index.html on onload to display the contents of a.txt. I figure that since a.txt will never be large, leaving it to the client is fine.
But I can't figure out how to do this and the W3C File API isn't offering me answers.
If I had to guess, somehow making sure index.html loads a.txt and then grabbing that via the file API might be the way to go but I'm not sure how to do that.
And I'll admit it, I'm a bit of a noob. If I'm invalidating browser sandbox or doing something impossible, please tell me. I just thought this would be so simple.
Also, I'd appreciate that if you were going to suggest AJAX, either don't, or explain it like I'm a baby because I really don't know.
Thank you all so much for your help.
Why file API is irrelevant:
Web applications should have the ability to manipulate as wide as possible a range of user input, including files that a user may wish to upload to a remote server or manipulate inside a rich web application.
From W3C File API.
So, File API is intended to be used to allow users to upload files from their clients into the server. On the other hand, AJAX is used to allow users to download files and other data from the server into their clients. And this is exactly what you need.
Refer to jQuery's ajax documentation.
I believe this page should help you out with your problem.
http://www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html#fbid=YhNukIHynD3
I would suggest using an Ajax call to the file on the server, since the response of the call will typically be the contents of that file.
Using Jquery this can be done by a simple
$.ajax({ 'url':'a.txt',
'success': function(r){
//display to innerDOM here, using r as the file
});
});
You simply want to display a txt file on the web page?
Do you know about server side includes?
That would be one possibility if you control the server.
If you really want to do it in javascript, then AJAX would be the way to go.
If it were me at that point I would figure out how to include and use jQuery to help with the ajax bits.
You will simply request the text file via its URL (you can get it to load in the browser right?), and then use jQuery to put that text into some DOM element.

Get filesname through javascript

How can I get list of filenames from a specified folder through javascript?
Assuming the folder you're talking about is on the client, then you can't. It'd bring up all sorts of security issues if JavaScript had that power.
If the folder is on the server then you could use some form of AJAX, with the server side code fetching the files in the specified directory and returning it back to the client.

Categories