英文:
nodejs specific route not displaying sql query
问题
我是新手学习JavaScript和Node.js,我有一个问题
我有一段代码,我正在创建一个简单的API,但我对为什么会发生这种情况感到非常困惑
```javascript
app.get('/api/list_all', (req, res) => {
get_info(`SELECT * FROM users;`, function(result){
res.json(result);
});
});
我有这个路由,用于在本地主机的MySQL上为我列出所有用户。连接是有效的,但当我输入该路由时,它返回一个空白屏幕和"[]"(空列表)
控制台和HTML页面没有显示任何错误
但当我将路由更改为"list_all"而不带有"api"部分时,它正常工作。像这样:
app.get('/list_all', (req, res) => {
get_info(`SELECT * FROM users;`, function(result){
res.json(result);
});
});
它会返回SQL响应。
问题是:为什么在路由中包含"api"时不起作用?
"get_info"函数:
function get_info(sql_data, callback){
con.getConnection((error, conn) => {
if (error) { throw error; }
conn.query(
sql_data,
(error, resultado) => {
conn.release();
if (error) { throw error; }
return callback(resultado)
}
)
})
}
我期望"api/list_all"路由能够正常返回包含所有用户的SQL结果。
<details>
<summary>英文:</summary>
I'm new to javascript and nodejs and i have a question
I have a code who i'm working in a simple api and i'm getting a lot of trouble to understand why this is happening
app.get('/api/list_all', (req, res) => {
get_info(SELECT * FROM users;
, function(result){
res.json(result);
});
});
I have this route to list all the users for me on screen for a local host mysql. The connection are working but when i type the route, returns me a blank screen with "[]" (empty list)
The console and the html page does not display any errors
But when i change the route for just "list_all" without the "api" part, it goes well. Like this:
app.get('/list_all', (req, res) => {
get_info(SELECT * FROM users;
, function(result){
res.json(result);
});
});
It returns me with the SQL response.
The question is: why is not working when i give the "api" on route?
The function "get_info":
function get_info(sql_data, callback){
con.getConnection((error, conn) => {
if (error) { throw error; }
conn.query(
sql_data,
(error, resultado) => {
conn.release();
if (error) { throw error; }
return callback(resultado)
}
)
})
}
I was expecting the route "api/list_all" to return the SQL result with all users normally.
</details>
# 答案1
**得分**: 0
You are probably importing wrong your code to your main server file
If the code shown above is in a different file from your **server** file you should add in the server.js the line `app.use('/api/list_all', imported_route);` and then change your code to
app.get('/', (req, res) => {
get_info(`SELECT * FROM users;`, function(result){
res.json(result);
});
});
For more help refer to previous question https://stackoverflow.com/questions/59418197/routing-not-working-in-node-js-and-express-js-api
<details>
<summary>英文:</summary>
You are probably importing wrong your code to your main server file
If the code shown above is in a different file from your **server** file you should add in the server.js the line `app.use('/api/list_all',imported_route);` and then change your code to
app.get('/', (req, res) => {
get_info(`SELECT * FROM users;`, function(result){
res.json(result);
});
});
For more help refer to previous question https://stackoverflow.com/questions/59418197/routing-not-working-in-node-js-and-express-js-api
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论