Sending a JSON file to Express server using JS

后端 未结 4 1088
难免孤独
难免孤独 2021-02-07 14:03

I am fairly new to JS and I have a JSON file that I need to send to my server (Express) that I can then parse and use its contents throughout the web app I\'m building.

4条回答
  •  时光说笑
    2021-02-07 14:44

    Read the file first and then send the json to the client.

    fs.readFile('file_name.json', 'utf8', function (err, data) {
      if (err) throw err;
      obj = JSON.parse(data);
      res.send(JSON.stringify(obj));
    });
    

提交回复
热议问题