0

I am new to Node.Js. I created an application with Node.JS Express. now I have below folder structure

-core
  app.js
  -routes
     index.js
  -views
     index.hbs

I want to store a json file into client pc. I want to write below piece of code into hbs file but it is not possible. would you mind explain to me how or where should I write it[in above structre]? It would be nice of you if you can share some link or tutorial that I can read and understand how to handle this kind of situation.

'use strict';

const fs = require('fs');
let data = JSON.stringify(ComponentItem);
fs.writeFileSync('file.json', data, finished);

Please take note that my Json data created in index.hbs. basically, I have the problem how to transfer objects between index.hbs and index.js or I am not sure I am doing right approach!

Amir
  • 1,919
  • 8
  • 53
  • 105

1 Answers1

0

Writing an HTTP service in Node allows you to run JavaScript and access Node APIs on the computer where the HTTP service is running.

It does not allow you to access Node APIs on the computer where the HTTP client is running.

You cannot use fs.writeFileSync to write a file to the hard disk belonging to the computer the browser is running on.

You can make an HTTP response with Content-Type: application/json, Content-Disposition: attachment, and JSON in the body. The browser will then save it. You wouldn't use an hbs file for that though. See this question.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • so all this even should go to index.js? do I need to use post? I still couldn't figure it out how to send my request from index.hbs to index.js without refresh the screen. whenever I do that my screen gets a refresh. I don't want that is happening – Amir Oct 18 '18 at 14:56