Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Node.js Express Google Search Autocomplete API Example to Fetch Data & Download it in CSV File Using EJS in Javascript

Posted on February 3, 2023

 

 

Welcome folks today in this blog post we will be using the google search autocomplete api to fetch data and download it in csv file using ejs. All the full source code of the application is shown below.

 

 

LIVE DEMO

 

 

Get Started

 

 

In order to get started you need to initialize a new node.js project using the below command as shown below

 

 

npm init -y

 

 

npm i express

 

 

npm i ejs

 

 

npm i suggestion

 

 

After that you will see the below directory structure of the node.js app as shown below

 

 

 

 

 

And now you need to create the index.js file and copy paste the following code

 

 

index.js

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var suggest = require('suggestion');
const express = require('express')
const bodyparser = require('body-parser')
const fs = require('fs')
 
const app = express()
 
app.use(bodyparser.urlencoded({extended:false}))
app.use(bodyparser.json())
 
app.set('view engine','ejs')
 
app.get('/',(req,res) => {
    res.render('index')
})
 
app.listen(5000,() => {
    console.log("App is listening on port 5000")
})

 

 

As you can see we are importing all the packages at the top and then we have the simple / route where we are loading the index.ejs file. Now we need to create the views folder and inside it we need to create the index.ejs file and copy paste the following code

 

 

views/index.ejs

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Google & Youtube Keyword Research Tool</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <h1 class="text-center">Google & Youtube Autocomplete Keyword Tool</h1>
        <form action="/getsuggestions" method="post">
            <div class="form-group">
                <label for="query">Enter Search Query:</label>
                <input class="form-control" type="text" name="query" placeholder="Enter Query" required><br>
                <button class="btn btn-danger btn-block">Download Results in CSV File</button>
            </div>
        </form>
    </div>
</body>
</html>

 

 

As you can see in the above html code we have the simple form where we have the input field where we allow the user to search for a specific term and then we have the button to submit the form. And here we are making a post request to the /getsuggestions endpoint. And now we need to write the post request inside the index.js file as shown below

 

 

index.js

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
app.post('/getsuggestions',(req,res) => {
    let outputfile = Date.now() + "output.csv"
    let query = req.body.query
    suggest(query,{ levels: 1 }, function (err, suggestions) {
        if (err) throw err;
        console.log(suggestions);
        const dataString = suggestions.join('\n');
        fs.appendFile(outputfile, dataString, function (err) {
            if (err) throw err;
            console.log('Data appended to file');
            res.download(outputfile,() => {
                
            })
        });
    })
})

 

 

As you can see we are getting the query entered by the user and then we are using the suggestion package and inside it we are passing the query that the user has searched for and then we are scraping all the autocomplete suggestions from google and then we are inserting the results into a csv file and download it as an attachment as shown below

 

 

 

Recent Posts

  • Android Java Project to Merge Multiple PDF Documents From Gallery Using iTextPDF Library
  • Android Java Project to Detect System Hardware & System CPU Info & Display inside TextView Widget
  • Android Java Project to Integrate Google OAuth2 Login & Logout System & Save User Info in SharedPreferences
  • Android Java Project to Export Raw Text to PDF Document Using iTextPDF Library
  • Android Java Project to Export Images From Gallery to PDF Document Using iTextPDF Library
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme