Node.js — how to get content of Google Drive API in the fastest way possible

I'm trying to make a website with Node.js and the Google Drive API. To do that, after getting my API key from google, I'm able to go to a link like this:{my folder ID}%27+in+parents&key={my API key}

and I get a response like this:

 "kind": "drive#fileList",
 "incompleteSearch": false,
 "files": [
   "kind": "drive#file",
   "id": "1MhS55q3ZmRyUxo_tPW3kqXuLIy-cAv1-",
   "name": "Capture001.png",
   "mimeType": "image/png"
   "kind": "drive#file",
   "id": "18y88x-DxOW4APcz6YGBFPVCeBB7eLPHC",
   "name": "contact.html",
   "mimeType": "text/html"
   "kind": "drive#file",
   "id": "1voNKTQv-kbyS-jJnsIXc-1OH1YudB3Xw4Dcvi6VKIqY",
   "name": "test",
   "mimeType": "application/"
   "kind": "drive#file",
   "id": "1QHbCATkynjDidEBjueVv4Qvh0ijfzV0S",
   "name": "Copy of 5737-12, 5738-01 (Freidin).mp4",
   "mimeType": "video/mp4"
   "kind": "drive#file",
   "id": "1u6VsnhYvvnEJlFXi_B7I-7DY4JkZP9mQ",
   "name": "Copy of 5750-01-03 Mincha-Maftir, Sicha and dollars.mpg",
   "mimeType": "video/mpeg"

and then if I were to (theoretically) get an ID of a particular file, then I could plug that into this URL:{my file ID}?alt=media&key={my api key}

SO I'm trying to make a website out of this with node.js, that when someone goes to my website /somename, then that "somename" accesses values in a dictionary with the ID of the main folder in Google Drive, with the API key, and then is able to theoretically access all files / folders of that folder with mysite/somename/file.png or /somename/contact.html or something to display html pages, here's my complete nodejs attempted code so far (doesn't support sub directories yet), you can try it with your own google drive folder and API key, just plug it in to the database object:

var http = require("http");
var fetch = require("node-fetch");

var gDriveFolder = (folderID, apiKey) => `${folderID}%27+in+parents&key=${apiKey}`
var gDriveFile = (fileID, apiKey) => `${fileID}?alt=media&key=${apiKey}`

var database = {
        "coby": {
            "apiKey":"{paste your API key here}",
            "folder":"{paste the shared URL of your Google Drive folder here}"
var server = http.createServer((request, response) => {

    var urlParts = request.url.split("/").filter(x => x.length > 0).map(x => x.split("%20").join(" "));

    if(urlParts[0] != "favicon.ico") { //to not make more than neccesary requests
        var html = "";
        var json = database;
        if(json) {
            console.log("We have a JSON!", json);
            if(urlParts.length > 0) {
                var folderWentTo = urlParts[0];
                var myData = null;
                for(var k in database) {
                    if(k == folderWentTo) {
                        myData = json[k];
                    console.log(k, ":", json[k]);


            if(myData) {
                var key = myData["apiKey"];
                var mainFolderURL = gDriveFolder(IDfromDriveURL(myData["folder"]),key);
                console.log("main folder: ", mainFolderURL);
                myReq(mainFolderURL, (data) => {
                    var fileList = JSONorNull(data);
                    if(fileList) {
                        var html = "<b>nothing</b><br>";
                        var f = fileList["files"];
                        if(f) {
                            if(urlParts.length > 1) {
                                var fileObject = f.filter(x=>x["name"] == urlParts[1])[0];

                                if(fileObject) {
                                    var fileID = fileObject["id"];
                                    var fileRequestedURL = gDriveFile(fileID, key);
                                    console.log("trying to get contents of ", fileRequestedURL);
                                    myReq(fileRequestedURL, (fileData) => {
                                        html = fileData;
                                     //   console.log("file: ", fileObject["name"], "'s content: ", fileData);
                                    //    response.setHeader("Content-Type",fileObject["mimeType"]);
                                } else {
                                    html = "file not found!";
                                    console.log("tried to find: ", urlParts[1], " in list: ", f);

                            } else {
                                f.forEach((y, o) => {
                                //    var fileURL = gDriveFile(y["id"], myData["apiKey"]);
                                    html += `<a href="${urlParts[0]}/${y["name"]}">${y["name"]}</a><br>`

                        } else {

                    } else {
                        response.end("this data: " + data + " went wrong");
            } else {
                html = ("asdf")
            console.log("url parts:", urlParts);

        } else {
            console.log("no json :(");
            response.end("dude what happened with the JSON: " + json + ", from " + bdy);




function myReq(url, cb) {
    var start = parseInt(;
    console.log("started", start);
    fetch(url).then(res => res.text()).then(body => {
        var end = parseInt(;
        console.log("ended at",end, " total:",(end-start));
       if(cb) {


function JSONorNull(str) {
    var result = null;
    try {
        result = JSON.parse(str);
    } catch(e) {
        console.log(str, " has an error: ", e);
    return result;

function IDfromDriveURL(url) {
    return url.split("/").length > 1 ? (url.split("/").filter(x=>x.includes("open"))).join("").replace("open?id=", "") : url;

the problem: currently I'm using node-fetch to get the content, as you can see in the myReq function, but this is seemingly taking a LONGLONG time, IDK if it's just the Google API, but I doubt it, I think it's how I'm getting the content... When I try to get a simple 800kb picture with localhost/coby/Capture1.png, its printed in my console (form the myReq function):

started 1550048095674
ended at 1550048130691  total: 35017

that means it took 35 seconds to load (and automatically download) the picture... Also just for the main page localhost/coby, the result is around 2500ms to 4440ms....

SO clearly either something's off with the drive API (which I doubt) or node-fetch is just acting very slow..

SO: what's the absolute fastest way to get the content of a page (and multiple pages, like what I'm doing) in node.js? I tried the request module, but it's also slow and supposedly fetch is supposed to be faster...

Try testing it with your own google drive folder and let me know if it works fast or not.

UPDATE BTW if I simply copy this function (and run it):

function coby2() {
    var startTime =;
    const drive_url = "{my file id}%27+in+parents&key={myAPIkey}";
    let drive_request = {
        method: "GET",
        /*headers: new Headers({
            Authorization: "Bearer "+access_token
    fetch(drive_url, drive_request).then( response => {
        var txt = response.json();
        console.log(response, txt);
    }).then( list =>  {
        var endTime =;
        console.log("Found files:",list.files, " with time total: ", (endTime - startTime));

my response time is about 150-300ms, and if I copy the exact same function into Node.js and call it in the request/response function (in the http.createServer function), the response is still about 4000ms on average... I think it's something with the node-fetch module.



Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.