How to return data from Postgresql pool in NodeJS?

Hi! I don’t understand how to return data from pool in Postgresql: I created a pool and http-server. Now, I need to fetch data from it in React, but my code on front doesn’t work:

  useEffect(() => {
        axios.get('http://localhost:5000/data').then(res => console.log(res));
    }, [])

const server = http.createServer(async (req, res) => {

    if (req.url === "/data" && req.method === "GET") {
        const results = await pool.query(`SELECT * FROM data LIMIT 1`);
        return JSON.stringify(results.rows);

    // No route present
    else {
        res.writeHead(404, { "Content-Type": "application/json" });
        res.end(JSON.stringify({ message: "Route not found" }));

server.listen(PORT, () => {
    console.log(`server started on port: ${PORT}`);

Sorry, I found an answer, you can delete this question. Thank you

You could post the answer up here, in case it helps anyone in the future.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.