Sending a JSON file to Express server using JS

后端 未结 4 1102
难免孤独
难免孤独 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:20

    Another option is to use sendFile and set the content type header.

    app.get('/search', (req, res) => {
        res.header("Content-Type",'application/json');
        res.sendFile(path.join(__dirname, 'file_name.json'));
    })
    

    The code assumes the file is in the same directory as the JS code. This answer explains how this works.

提交回复
热议问题