How do you use npm fs? - javascript

I'm trying to make a Discord bot and I want to store a boolean value that will stay the same even when my bot restarts, I'm pretty sure I need fs to do this but I can't figure out how to use it, and I'm not successfully finding any documentation for it on github or npm...
So how would I go about storing a variable in a javascript file? (Or JSON if I need to)

Use for example LowDB LowDB. Super light-weight and allows easy read/write to a simple JSON file. All your storing is a simple boolean? Seems overkill to setup a full db engine.

Hesitant to provide a solution here...You should NOT store values this way - you need to utilize a DB. Jonas' solution will work - but consider the DB route going forward.

Related

How to save my Nodejs BLockchain

In the last few weeks, I tried to code my own Blockchain, just to understand the whole concept better.
You can find my code here: https://github.com/Snixells/js-blockchain .
I already implemented that the Blockchain + Transactions are created through nodeJs arrays and JSON.
The problem I am working on right now is that the data does not get saved. I want to run the whole blockchain on a (maybe) express server and access it by a RESTful API. Because of that, I need a way to store the Blockchain somewhere. I also have already some ideas but none of them seems like a good one.
I could save the whole chain as a JSON file and always when needed open that and afterwards save it. But that won't scale at all later
I thought about saving each block as a single JSON file, but I think that wouldn't work that great either.
I could use any kind of database, like RethinkDB or MongoDB but that conflicts with the whole idea of Blockchain being the database itself.
I would love to hear some answers, for example, what frameworks and so on I could use. Or maybe any ideas on how to store the database at all.
Thanks for your help :)
Update:
I tried out rethinkDB and it seems to be a great choice because you can just simply store json objects in that database. It's perfect for what I need!

client side scripting language that enables storing data

I am creating a browser extension that has to store data.
I am going to have the client side language read from the page which is a game,
and save data if mistakes were made , or the data is not already accessible.
basically i'm trying to make a bot for a choice decision game when the
posibilities and the questions are built in.
since the possibilities are final I would really like it if in some way i would be
able to actually make the bot play for an hour and then have a file containing
the entire game's deicisions.
well , I can't do that with javascript since it disables storing data,
am I able to do it with another language which has access to the html DOM?
I have no problem to even use some language that wasn't supposed to do that
and then write a small library for DOM accessibility, as long as it allows DOM
access, I have no idea which though.
edit: I haven't noticed I actually got answers to this. I realize the question is very vague(it's a very old one), but basically, I just built a simple html parser via python. I asked a friend of mine how I could build a bot, he said he simply did it by creating a browser extension, and I decided I'll give it a shot. anyways, yes, I should've resourced more into the browser's api and check for a way to store client's data.
I'm not sure to clearly understand what you want to do but maybe you could use the local storage of your browser with Javascript. It's a simple way to store a little amount of data in a webApp context.
EDIT 1 :
Here's a little sample to help you :
// Store
localStorage.score = 5000;
// Retrieve data
var score = localStorage.score;

Strategy for testing POST to API without changing database

I'm using jasmine-node to test my API, and it has worked great for my GET routes. Now, however, I need to test some POSTs and I'm not sure how to go about this without changing my database.
One thought I had was to reset whatever value I change at the end of each spec.
Is this reasonable or is there a better way to go about testing POST requests to my API?
Wrap anything that modifies your database into a transaction. You can have your database changes and then rollback after each test.
usually you are supposed to have a test database, so modify that one is not a big issue. also, a general approach would be not to rely on predefined values on the database (i.e, the GET always request the SAME object..) but try with different objects each time. (using predefined objects may hide problems when the data is slighty different..).
in order to implement the second strategy, you can execute a test with a POST with pseudo-random data to create a new object, and use the returned ID to feed the following GET, UPDATE and finally the DELETE tests.
Just make a duplicate processing page/function and send the data to that for debugging. Comment out anything that makes changes to the database.
Alternatively, pass a variable in your call such as "debug" and have an if/else section in your original function for debugging, ignoring the rest of the function.
Another alternative still is to duplicate your database table and name it debug table. It will have the same structure as your original. Send the test data to it instead and it won't change your original database tables.
I'm pretty sure that you've come up with some solution for your problem already.
BUT, if you don't, the Angular $httpBackend will solve your problem. It is a
Fake HTTP backend implementation suitable for unit testing applications that use the $http service.

Can I make Rails' CookieStore use JSON under the hood?

I feel like it should be obvious doing this from reading the documentation, but maybe somebody can save me some time. We are using Ruby's CookieStore, and we want to share the cookie with another server that is part of our website which is using WCF. We're already b64-decoding the cookie and we are able to validate the signature (by means of sharing the secret token), all of that is great... but of course the session object is marshalled as a Ruby object, and it's not clear what is the best way to proceed. We could probably have the WCF application make a call to Ruby and have it unmarshal the object and write it out as JSON, but that seems like it will add an unnecessary layer of complexity to the WCF server.
What I'd really like to do is maybe subclass CookieStore, so that instead of just b64 encoding the session object, it writes the object to JSON and then b64's it. (And does the reverse on the way back in, of course) That way, the session token is completely portable, I don't have to worry about Ruby version mismatches, etc. But I'm having trouble figuring out where to do that. I thought it would be obvious if I pulled up the source for cookie_store.rb, but it's not (at least not to me). Anybody want to point me in the right direction?
(Anticipating a related objection: Why the hell do we have two separate servers that need to be so intimately coordinated that they share the session cookie? The short answer: Deadlines.)
Update: So from reading the code, I found that when the MessageVerifier class gets initialized, it looks to see if there is an option for :serializer, and if not it uses Marshal by default. There is already a class called JSON that fulfills the same contract, so if I could just pass that in, I'd be golden.
Unfortunately, the initialize function for CookieStore very specifically only grabs the :digest option to pass along as the options to MessageVerifier. I don't see an easy way around this... If I could get it to just pass along that :serializer option to the verifier_for call, then achieving what I want would literally be as simple as adding :serializer => JSON to my session_store.rb.
Update 2: A co-worker found this, which appears to be exactly what I want. I haven't gotten it to work yet, though... getting a (bah-dump) stack overflow. Will update once again if I find anything worthy of note, but I think that link solves my problem.

What is node-lru-cache?

what is node LRU cache? Anyone can explain how to implement it? Lets say I have three layers, client-midlayer(handle calls)-backend(mongoDB), and the LRU cache should be implemented in the midlayer.
Would be nice is there is a simple example just showing how it works! Thanks in advance.
There's an example on how to use it within the source repository: https://github.com/isaacs/node-lru-cache/tree/master/test
I'm assuming you want the LRU to persist to MongoDB? If that's the case, you'll need to extend or rewrite the library, as it looks like a simple in-memory LRU cache module at first glance.
You'll also want to consider Redis' sorted set for this. If you have multiple frontend server instances, keeping a single LRU instance per server will lead to them getting out of sync. Redis's sorted sets are a natural fit for this problem and are extremely fast.
You can use a timestamp to keep them ordered by most recent, and the list can be read and updated atomically via transactions. It will definitely suit the purpose of a cache well.

Categories

Resources