0

How can i run this code from angular2 app component?

var request = require("request");
var fs = require("fs");

var img = "http://vignette1.wikia.nocookie.net/custombionicle/images/a/ae/Kitten_with_gun_final.png";

request(img).pipe(fs.createWriteStream('image.png'));
3
  • 2
    You can't, your AngularJS code will be executed client side, NodeJS is server side. Commented Jan 21, 2017 at 19:35
  • is there a way to implement this using angular2? Commented Jan 21, 2017 at 19:39
  • There is no way to write files to the user disk using client side javascript (for obvious security reasons), as suggested by Razvan Alex, you can write this code in your backend. Commented Jan 21, 2017 at 19:47

1 Answer 1

3

Unfortunately, node.js is the backend part of the website and angular the frontend, you can't mix them together. What you can do instead is to run the pice of code on the backend and if there's some data you need it on the client side, to send it to the client ;)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.