2

I am trying to implement something like "you are visitor number n" in my angular 6 app. I am not using a database, so the best thing is to store the number in a file in the assets folder say, then read the number off of it, update and store the incremented number.

Reading is easy, as in:

this.http.get("/assets/visits").subscribe(vn => {
  this.visitNums = +vn + 1;
});

But I am having a problem writing to the file. I tried:

fs.writeFile("/assets/visits, 'whatever',
             err => console.log(err));

But I get a "fs module not found" even with:

import * as fs from "fs";

And also, doing a:

declare const fs: any;

given "fs is not defined" on that fs.writeFile line.

So, is there a way to write to a file from within an Angular 6 app?

5
  • As @abdullahkady mentioned, this is not possible. Consider instead using a cloud "database" such as Firebase to store this value. Firebase and Angular work well together. check out the tutorial here: medium.com/factory-mind/… Commented Aug 13, 2018 at 19:56
  • I know how to do it with databases with Mongo or Firebase being best options. But, for just a single number to store and fetch between reads, it's not worth it as I am also not paying anything now. Commented Aug 13, 2018 at 19:59
  • I didn't think either database required paying anything? Commented Aug 13, 2018 at 20:00
  • Thought they do! Right now I am deploying an app on heroku for free. Setting up a database connection with some provider should, although maybe not for something insignificant like for just visits number. Still, I would like to do it with files if possible. Commented Aug 13, 2018 at 20:07
  • Apparently there is a free level: firebase.google.com/pricing. Thanks. Commented Aug 13, 2018 at 22:17

1 Answer 1

5

What you're trying to do is not possible at all, even as a concept. The "assets" folder resides on your own server, you can't let a client write to it simply when visiting from another machine, which means he will have no access to your file system (hence the "fs", which is a node.js module by the way, meaning it's code won't be present in the browser).

Sign up to request clarification or add additional context in comments.

4 Comments

Fine, how? And frankly I don't know why the two upvotes. I asked: is there a way? And, I gave the ways I used that didn't solve my problem, and they did involve the fs being mentioned here.
No way to achieve that purely on the client end of the application, you will have to make use of any server storage. As @Deborahk mentioned, firebase is one of the easiest services that you can use for your case
Never meant client end here, the question is asking for if a value can be stored and manipulated in files instead of databases.
You know what, you are "maybe" right. When fasting, you start missing some important articles. I read your "not possible at all" as "very possible of course"! How? I have no idea. Now I get what you said from what DeborahK is saying. Forgive my misread and my wondering why the two upvotes. +1 and a Right from me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.