Explorer App

Postrgresql + Nodejs + Expressjs + Reactjs

Once we have Postgresql running, we need an application that consumes from the database.

Backend: Nodejs + Expressjs

const Pool = require('pg').Pool

const pool = new Pool({
    user: 'postgres',
    host: '<ip-address>',
    database: 'cexplorer',
    password: '<password>',
    port: 5432,
})


const getMetadata = async (callback) => {
    await pool.query('SELECT * FROM meta', callback)
};

app.get('/metadata', function (req, res) {
    getMetadata((error, result) => {
        if (error) {
            throw error;
        }
        res.send(result);
    })
});

Frontend: Reactjs

const requestOptions = {
            method: 'GET',
            headers: {'Content-Type': 'application/json'}
        };
fetch('http://localhost/metadata', requestOptions)
    .then(response => response.text())
    .then(data =>
        this.setState({
            metadata: JSON.parse(data)
        }, () => {
            console.log(this.state.metadata);
        })
    );

Última actualización

¿Te fue útil?