0

I am really new to Node and Express, for this project I was trying to post values from the node server to google Sheets which is working. The only issue I am facing now is, to post the value I always have to reload the browser or use the localhost:3000 in the browser. Is there a way that we can post the value when the node server is started?

Index.js Code

const express = require("express");

const { google } = require("googleapis");



const app = express();

var req = http.request(options, callback);

    app.get("/", async (req,res) => {
    
    const auth = new google.auth.GoogleAuth({

        keyFile: "credentials.json",
        scopes: "https://www.googleapis.com/auth/spreadsheets",

    });

    const client = await auth.getClient();


    // Instance of Google Sheets API

    const googleSheets = google.sheets({version: "v4", auth: client});


    const spreadsheetId = "1SwZvn0L_wqswv";

    // get meta data about spreadsheet

    const metaData = await googleSheets.spreadsheets.get({

        auth,

        spreadsheetId,


    });

    await googleSheets.spreadsheets.values.append({

        auth,

        spreadsheetId,

        range: "Sheet1!A:B",

        valueInputOption: "USER_ENTERED",

        resource: {

            values: [

                ["Man", "PostSheet"],

            ] // end of values array
        }
    })


});


app.listen(3000, (req,res) => console.log("running on 3000"));

Reference Link

As for reference I used the link from stackoverflow. I was able to post data using the link but unable to post data without using a browser. Basically I just want to post the data when the server is started.

Yahoo
  • 558
  • 11
  • 34
  • Without `app.listen` ,how will your server even run? – Shubham Dixit May 20 '22 at 17:39
  • using node index.js – Yahoo May 20 '22 at 17:40
  • Just use [nodemon](https://www.npmjs.com/package/nodemon) – DepletedKnowledge May 20 '22 at 17:40
  • @DepletedKnowledge nodemon will restart the server. I want to post the data without using app.listen or running the browser. Is there a way to do that ? Currently I start the server and run localhost:3000 in browser to post the data. I just want to run the server and post the data. Is there a way to do that ? – Yahoo May 20 '22 at 17:44
  • 1
    I don't think you understood the job of a node/express server, that server is just to host code, not make post requests, you can use postman for that, there is no need to have a server running. Maybe I misunderstood your problem? – DepletedKnowledge May 20 '22 at 17:49
  • You want to post data to your server as soon as it's started? – Matt Oestreich May 20 '22 at 17:53
  • @MattOestreich run a post function as soon as the server starts. – Yahoo May 20 '22 at 17:53
  • You can host code in a server that periodically make post requests somewhere, but you can't make post requests and then auto-restart. That is not it's job. – DepletedKnowledge May 20 '22 at 17:54
  • I run the server now and run localhost:3000 on the browser to post the data. Is there a way I can just run the server and post data through the server ? I am confused why I should use browser(localhost:3000) to post data . – Yahoo May 20 '22 at 17:55
  • The server is just to host code, you can just make a js file to make post requests and run that file every time you need to make a new one (if thats the case just use postman), if you need to periodically make post requests and want to automate the process then you'll need a server. – DepletedKnowledge May 20 '22 at 17:59
  • 1
    What is the underlying functionality you're trying to create because none of your question makes any sense so there's probably some underlying problem you're actually trying to solve, but we don't know what that is so can't help you with the best solution to that real problem. Right now, this reads like an [XY Problem](https://en.wikipedia.org/wiki/XY_problem) where you are sharing some problem with an attempted solution, but not sharing the underlying problem. – jfriend00 May 20 '22 at 18:01
  • I'm not sure the use case but why not just post to your own server in the app.listen callback? `app.listen(port, () => fetch("/", { method: "post", body: '...' })` note that you may need to use node-fetch. – Matt Oestreich May 20 '22 at 18:02
  • If you're going to use a server, then you must use `.listen()` to start that server otherwise nothing can connect to the server. But, perhaps your real problem doesn't need a server (if only you would share the real, underlying problem, then we could help with that). – jfriend00 May 20 '22 at 18:02
  • 1
    Doesn't even make sense to use that just so he can make HTTP requests, I think he needs a REST client, but without really explaining the problem we can't know – DepletedKnowledge May 20 '22 at 18:04
  • There is no need to post to your own server from your server - it's just extra code to run through http just to get to your own code. Instead, just put whatever code you want to execute in a shared function and then call that code directly from wherever you want to use it. – jfriend00 May 20 '22 at 18:06
  • @everyone I understand that my question was not clear. I just edited my question and added the index.js code that I am using. – Yahoo May 20 '22 at 18:07
  • 1
    Just put that code in a function and call it when you run your script. You don't need an http server just to execute your own code. – jfriend00 May 20 '22 at 18:08
  • 1
    What he means is you don't need express at all, just make a single js file/function with that code and run it with node. – DepletedKnowledge May 20 '22 at 18:12
  • @DepletedKnowledge any tutorial or link that I can follow ? – Yahoo May 20 '22 at 18:19
  • Nothing this specific, I think. You'll need to read the documentation for that API (being google, probably has excellent documentation). If all you want is to use what you wrote, just remove everything related to express and place everything else in an async function because you use await, then call that function. Run with `node index.js` in your terminal. I can make a more elaborate answer if you need, just place a comment. – DepletedKnowledge May 20 '22 at 18:51
  • Their API [documentation](https://developers.google.com/sheets/api/) – DepletedKnowledge May 20 '22 at 18:51
  • @DepletedKnowledge can you please elaborate answer. I am still not able to fix it. I tried to follow their document , configured the google sheet with the node js too but it says it will provide the access code when I login through browser and it does not provide the code. – Yahoo May 20 '22 at 21:14
  • See if that will solve the problem for you. Note, you'll have to run the script every time you need to make a new "request" – DepletedKnowledge May 20 '22 at 22:44

1 Answers1

0

Remove everything related to express and add everything else to an asynchronous function (because you're using await), then call that function in the same file. Example:

const { google } = require("googleapis");

async function FUNCTIONNAMEHERE () {
    const auth = new google.auth.GoogleAuth({
    
        keyFile: "credentials.json",
        scopes: "https://www.googleapis.com/auth/spreadsheets",
    
    });
    
    const client = await auth.getClient();
    
    
    // Instance of Google Sheets API
    
    const googleSheets = google.sheets({version: "v4", auth: client});
    
    
    const spreadsheetId = "1SwZvn0L_wqswv";
    
    // get meta data about spreadsheet
    
    const metaData = await googleSheets.spreadsheets.get({
    
        auth,
    
        spreadsheetId,

    });
    
    await googleSheets.spreadsheets.values.append({
    
        auth,
    
        spreadsheetId,
    
        range: "Sheet1!A:B",
    
        valueInputOption: "USER_ENTERED",
    
        resource: {
    
            values: [
    
                ["Man", "PostSheet"],
    
            ] // end of values array
        }
    })
}

FUNCTIONNAMEHERE()

After that run the file with node index.js