Skip to content
Advertisement

store sql queries as string in node server to get them as a response(express)

I am trying to do something may or may not be possible. I have a SQL file called “travel.sql” that I am trying to make an api out of, so I thought the simplest thing to do is to save the queries as strings in an array and then save the array of strings as a response for a node server(express.js)

so simply here’s the code till now but this is returning nothing in postman and I don’t know what’s missing or not I checked all the packages and they are installed properly

const express = require('express')
const fse = require( "fs-extra" );
const { join } = require( "path" );

const app = express()
const port = 3000

app.get('/sqlfile', (req, res) => {
    const loadSqlQueries = async folderName => {
        // determine the file path for the folder
        const filePath = join( process.cwd(),  travel );
     
        // get a list of all the files in the folder
        const files = await fse.readdir( filePath );
     
        // only files that have the .sql extension
        const sqlFiles = files.filter( f => f.endsWith( ".sql" ) );
     
        // loop over the files and read in their contents
        const queries = {};
        for ( let i = 0; i < sqlFiles.length; i++ ) {
            const query = fse.readFileSync( join( filePath, sqlFiles[ i ] ), { encoding: "UTF-8" } );
            queries[ sqlFiles[ i ].replace( ".sql", "" ) ] = query;
            console.log(queries)
        }
        return queries;
        res.send(queries);
     };
     
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

Advertisement

Answer

I’m not quite sure of what you are trying to achieve, But anyway You have multiple parts of your code need to be enhanced:

  1. As a first proposal I suggest to add a “try and catch” to your code so you can know the errors you are facing.

  2. You are creating a function expression “loadSqlQueries” which I think is not needed and it never runs as you are just creating it but you never used it.

  3. As the function expression is not needed then also the “return” is not needed.

  4. To be able to use “await” like here: const files = await fse.readdir( filePath ); You need to use it inside “async” function.

  5. You are using “travel” here const filePath = join( process.cwd(), travel ); as a variable, you need to use it as a string like this const filePath = join( process.cwd(), "travel" );

  6. I’ve applied the above mentioned changes, kindly read the comments I added to your code to catch the changes and here is the final code:

const express = require('express')
const fse = require("fs-extra");
const { join } = require("path");

const app = express()
const port = 3000

app.get('/sqlfile',
    // add async to be able to use await
    async (req, res) => {
        // add try and catch block to your code to catch the errors
        try {
            // no need for the function expression which is never used
            // const loadSqlQueries = async folderName => {

            // determine the file path for the folder
            //use travel as a string not a variable
            const filePath = join(process.cwd(), "travel");

            // get a list of all the files in the folder
            const files = await fse.readdir(filePath);


            // only files that have the .sql extension
            const sqlFiles = files.filter(f => f.endsWith(".sql"));

            // loop over the files and read in their contents
            const queries = {};
            for (let i = 0; i < sqlFiles.length; i++) {
                const query = fse.readFileSync(join(filePath, sqlFiles[i]), { encoding: "UTF-8" });
                queries[sqlFiles[i].replace(".sql", "")] = query;
                console.log(queries)
            }

            // As the function expression is not used we will comment return
            //   return queries;

            res.send(queries);
            //   }
        } catch (error) {
            console.log(error);
        }


    })

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement