简体   繁体   中英

How to run a node js file (that makes a api call and puts data into a sheet) by clicking on a button in html

I tried to get some answers here on stack overflow but I don't really understand it.

I want to execute my node js script by clicking on a button in html. The node js file pulls data from an excel sheet and puts it into a specific sheet.

The node js file works perfectly if I run it in the console. But how do I connect this file with a button in html? It is not possible just to call the function, because the node js file is running on a server.

This is the function I want to execute by clicking on a button

function importData(){
    //import the smartsheet
    ss.sheets.importXlsxSheet(options)
    .then(function(result) {
        console.log("Created sheet '" + result.result.id + "' from excel file");
        
        // Load entire sheet
        ss.sheets.getSheet({ id: result.result.id })
            .then(function(sourceSheet) {
                console.log("Loaded: " + sourceSheet.rows.length + " rows from sheet '" + sourceSheet.name + "'");
                console.log("Done");

                    //copy every row from sourcesheet in a array 
                    const sourceSheetRows = sourceSheet.rows;
                    writeRowsinArray(result, sourceSheetRows);
            })
            .catch(function(error) {
                console.log(error);
            });
    })
    .catch(function(error) {
        console.log(error);
    });
}

The standard way to trigger something on a server from a browser is to make an HTTP request to it.

So set up an HTTP endpoint that will trigger the function (eg by running a server built around the express module or using a cloud service such as AWS Lambda) then use client-side JS to make the request (eg with the Fetch API).

You have to call an exposed api for that. Your nodeJs script have to listen on an endpoint.

From JavaScript in your html you have to call your api using fetch .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM