mohsenmadi
mohsenmadi

Reputation: 2377

Writing to a file in Angular 6

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?

Upvotes: 2

Views: 9437

Answers (1)

abdullahkady
abdullahkady

Reputation: 1071

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).

Upvotes: 5

Related Questions