因此,我在postgres和一些nodejs上设置了一个数据库,以便从数据库中获取数据,我最终没有收到任何错误,但现在数据不会显示,但没有收到任何错误。我的索引文件:
const port = process.env.PORT || 5000;
console.log(`Listening: http://localhost:${port}`);
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const db = require('./queries');
const { response } = require('express');
app.use(bodyParser.json())
app.use(
bodyParser.urlencoded({
extended: true,
})
)
app.get('/', (request, response) => {
response.json({ info: 'Node.js, Express, and Postgres API' })
})
app.get('/drinks', (req, res) => {
res.json(db.getDrinks);
});
app.listen(port, () => {
console.log(`App running on port ${port}.`)
})
此外,我有一个查询文件与所有的数据库连接和实际的sql功能。
const Pool = require('pg').Pool
const pool = new Pool({
stuff in here is good just removed it to hide database info
})
const getDrinks = (request, response) => {
pool.query('SELECT * FROM drinks', (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
}
module.exports = {
getDrinks,
}
所以基本上,当我运行npm,run,start时,localhost显示信息消息,但当我进入localhost/drinks时,我只得到一个空页面。如何获取要显示的数据?
更改app.get('/drings',(req,res)=>{res.json(db.getdrings);})
到app.get('/drinks',getDrinks)
,因为您已经将request
和resposne
对象传递给getDrinks
。虽然将req
和res
对象传递给getdrinks
函数不是很好的做法,但为什么?
另外,如果您没有使用端口80上运行的任何反向代理,则必须点击端口号为ielocalhost:port/drings
的URL