Server.js

From Subtle Meerkat, 4 Weeks ago, written in JavaScript, viewed 20 times.
URL http://codebin.org/view/656fed3b Embed
Download Paste or View Raw
  1. var express = require('express');
  2. var pg = require('pg');
  3. var app = express();
  4.  
  5. var pool = pg.Pool({
  6.         user: 'postgres',
  7.         host: '127.0.0.1',
  8.         database: 'test',
  9.         password: 'root',
  10.         port: '5432'
  11. });
  12.  
  13. app.get('/users', (req, res) => {
  14.         res.json([
  15.                 {id: 1, username: 'username1'},
  16.                 {id: 2, username: 'username2'}
  17.         ]);
  18. });
  19.  
  20. app.get('/students/', (req, res) => {
  21.         pool.query('SELECT * FROM Student', (err, result) => {
  22.                 err ? console.log(err) : res.send(result.rows);
  23.         });
  24. });
  25.  
  26. app.get('/students/:name', (req, res) => {
  27.         var query = "SELECT * FROM Student WHERE name like '%" + req.params.name + "%'";
  28.         pool.query(query, (err, result) => {
  29.                 err ?  console.log(err + '\n' + 'Query: ' + query) : res.send(result.rows);
  30.         });
  31. });
  32.  
  33. app.listen(4000, () => {
  34.         console.log('listening at localhost:4000');
  35. });

Reply to "Server.js"

Here you can reply to the paste above