Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express')
- const bodyParser = require('body-parser')
- const app = express()
- const Pool = require('pg').Pool
- const pool = new Pool({
- user: 'postgres',
- host: 'localhost',
- database: 'testesmartsensor',
- password: 'lauddev',
- port: 5432,
- })
- app.use(bodyParser.json())
- app.use(
- bodyParser.urlencoded({
- extended: true,
- })
- )
- // Retorna um JSON com todos os dados da tabela sensor.
- app.get('/sensorLog', (req, res) => {
- pool.query('SELECT * FROM sensor', (error, results) => {
- if (error) throw error;
- res.status(200).json(results.rows)
- })
- });
- // Insere dados na tabela sensor.
- app.post('/insertSensor', (req, res) => {
- console.log("QUERY: INSERT INTO sensor (nome, valor) VALUES ('" + req.body.nome + "'," + req.body.valor + ");");
- pool.query("INSERT INTO sensor (nome, valor) VALUES ('" + req.body.nome + "', " + req.body.valor + ");", (error, results) => {
- if (error)
- {
- console.log("ERRO: " + error);
- }
- res.send(JSON.stringify(results));
- });
- });
- app.get('/', (req, res) => {
- res.json({ info: 'Node.js, Express, and Postgres API' })
- })
- app.listen(4000, () => {
- console.log(`App running on port 4000.`)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement