将sql查询作为字符串存储在节点服务器中,以将其作为响应获取(express)
我正在尝试做一些可能不可能的事情。 我有一个名为“travel.SQL”的将sql查询作为字符串存储在节点服务器中,以将其作为响应获取(express),sql,node.js,rest,express,Sql,Node.js,Rest,Express,我正在尝试做一些可能不可能的事情。 我有一个名为“travel.SQL”的SQL文件,我正试图用它来制作一个api,因此我认为最简单的方法是将查询保存为数组中的字符串,然后将字符串数组保存为节点服务器(express.js)的响应 这是到目前为止的代码,但邮递员没有返回任何信息,我不知道遗漏了什么 我检查了所有的软件包,它们都安装正确了 const express = require('express') const fse = require( "fs-extra" );
SQL
文件,我正试图用它来制作一个api,因此我认为最简单的方法是将查询保存为数组中的字符串,然后将字符串数组保存为节点服务器(express.js)的响应
这是到目前为止的代码,但邮递员没有返回任何信息,我不知道遗漏了什么
我检查了所有的软件包,它们都安装正确了
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}`)
})
const express=require('express'))
常数fse=要求(“fs额外”);
const{join}=require(“路径”);
const app=express()
常数端口=3000
app.get('/sqlfile',(req,res)=>{
const loadsqlquerys=async folderName=>{
//确定文件夹的文件路径
const filePath=join(process.cwd(),travel);
//获取文件夹中所有文件的列表
const files=wait fse.readdir(文件路径);
//仅限扩展名为.sql的文件
常量sqlFiles=files.filter(f=>f.endsWith(“.sql”);
//在文件上循环并读取其内容
常量查询={};
for(设i=0;i{
log(`示例应用程序正在侦听http://localhost:${port}`)
})
我不太确定您想要实现什么,但无论如何,您的代码有多个部分需要改进:
const files=await fse.readdir(filePath)代码>您需要在“异步”函数中使用它
constfilepath=join(process.cwd(),travel)
作为一个变量,您需要将其用作如下字符串constfilepath=join(process.cwd(),“travel”)代码>
const express=require('express'))
常数fse=要求(“fs额外”);
const{join}=require(“路径”);
const app=express()
常数端口=3000
app.get(“/sqlfile”,
//添加async以便能够使用wait
异步(请求、恢复)=>{
//将try-and-catch块添加到代码中以捕获错误
试一试{
//不需要从未使用过的函数表达式
//const loadsqlquerys=async folderName=>{
//确定文件夹的文件路径
//将行程用作字符串而不是变量
const filePath=join(process.cwd(),“travel”);
//获取文件夹中所有文件的列表
const files=wait fse.readdir(文件路径);
//仅限扩展名为.sql的文件
常量sqlFiles=files.filter(f=>f.endsWith(“.sql”);
//在文件上循环并读取其内容
常量查询={};
for(设i=0;i{
log(`示例应用程序正在侦听http://localhost:${port}`)
})