Advertisement
Guest User

Untitled

a guest
Mar 12th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var db_config = {
  2.     user: 'marta', // name of the user account
  3.     password : 'ahX0jai3',
  4.     database: 'martadb', // name of the database
  5.     max: 10, // max number of clients in the pool
  6.     idleTimeoutMillis: 30000 // how long a client is allowed to remain idle before being closed
  7. }
  8. // call the packages we need
  9. var express    = require('express');        // call express
  10. var bodyParser = require('body-parser');
  11. var pg = require('pg');
  12. var format = require('pg-format');
  13. var app = express();
  14.  
  15. const client = new pg.Client(db_config);
  16. client.connect();
  17.  
  18. app.use(bodyParser.urlencoded({ extended: true }));
  19. app.use(bodyParser.json());
  20.  
  21. var port = process.env.PORT || 8080;        // set our port
  22. var router = express.Router();              // get an instance of the express Router
  23.  
  24. router.get('/preco/cidade/:id_cidade/quartos/:nquartos', async (req, res) => {
  25.     var query_preco = format('SELECT AVG(valor_venda) FROM anuncios WHERE num_quartos = %L and id_cidade = %L',req.params.nquartos, req.params.id_cidade);
  26.     const {rows} = await client.query(query_preco);
  27.     console.log(rows);
  28.     res.json(rows[0]);
  29. });
  30.  
  31. app.use('/marta', router);
  32. app.listen(port);
  33.  
  34. console.log('Magic happens on port ' + port);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement