Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //router
- 'use strict';
- const express = require('express');
- const router = express.Router();
- const controller = require('../controllers/index');
- //const connectionString = 'postgresql://postgres:root@localhost:5432/shop';
- //const client = new Client({connectionString});
- router.get('/', (req, res, next) => {
- controller.get((err, result) => {
- if(err){
- res.status(500).send({'Erro': err});
- }else{
- res.status(200).send({
- data: result
- })
- }
- })
- });
- router.get('/:id', controller.getId);
- module.exports = router;
- //controller
- const client = require('../../db/index');
- client.connect();
- exports.get = (callback) => {
- const re = [];
- client.query('SELECT nome FROM cliente', (err, result) => {
- if(err){
- //done();
- callback(err);
- }else{
- result.rows.map( r => {
- re.push(r.nome);
- });
- callback(null, re);
- }
- });
- };
- exports.getId = (req, res, next) => {
- const re = [];
- client.query('SELECT nome FROM cliente WHERE id=$1',[req.params.id], (err, result) => {
- if(err){
- //done();
- console.log(err);
- res.status(400).send('error no banco');
- }
- result.rows.map( r => {
- re.push(r.nome);
- });
- //client.end();
- res.status(200).send({'nome':re});
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement