Guest User

Untitled

a guest
Dec 28th, 2018
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. id | name | age | username
  2. -------+----------+----+---------------
  3. 1 | P | 38 | user1
  4. 2 | Q | 38 | user2
  5. 3 | R | 38 | user3
  6.  
  7. const express = require('express'),
  8. pg = require("pg");
  9.  
  10. const app = express();
  11.  
  12. const config = {
  13. user: 'appuser',
  14. database: 'users',
  15. password: '12345',
  16. port: 5432
  17. };
  18.  
  19. const pool = new pg.Pool(config);
  20.  
  21. app.get('/users', (req, res, next) => {
  22. pool.connect(function (err, client, done) {
  23. if (err) {
  24. done();
  25. console.log("Can not connect to the DB" + err);
  26. }
  27. client.query('SELECT * FROM Users ORDER BY id', function (err,
  28. result) {
  29. done();
  30. if (err) {
  31. console.log(err);
  32. res.status(400).send(err);
  33. }
  34. //res.status(200).send(result.rows[0]);
  35. return res.status(200).json(result.rows);
  36. })
  37. })
  38. });
  39.  
  40. app.listen(4000, function () {
  41. console.log('Server is running.. on Port 4000/users');
  42. });
  43.  
  44. export interface User {
  45. id: number;
  46. name: string;
  47. age: number;
  48. username: string;
  49. }
  50.  
  51. import { Injectable } from '@angular/core';
  52. import { Observable, of, throwError } from 'rxjs';
  53. import { HttpClient, HttpHeaders, HttpErrorResponse } from
  54. '@angular/common/http';
  55. import { catchError, tap, map } from 'rxjs/operators';
  56.  
  57. import { User } from './user';
  58.  
  59. const httpOptions = {
  60. headers: new HttpHeaders({'Content-Type': 'application/json'})
  61. };
  62. const apiUrl = "http://localhost:4000/users";
  63.  
  64. @Injectable({
  65. providedIn: 'root' //the service is available to the entire app
  66. })
  67.  
  68. export class UserService {
  69.  
  70. constructor(private http: HttpClient) { }
  71.  
  72. getUsers (): Observable<User[]> {
  73. return this.http.get<User[]>(apiUrl)
  74. .pipe(catchError(this.handleError('getUser', [])));
  75. }
  76.  
  77. getUser(id: number): Observable<User> {
  78. const url = `${apiUrl}/${id}`;
  79. return this.http.get<User>(apiUrl).pipe(
  80. tap(_ => console.log(`fetched User id=${id}`)),
  81. catchError(this.handleError<User>(`getUser id=${id}`))
  82. );
  83. }
  84. }
  85.  
  86. import { Component, OnInit } from '@angular/core';
  87. import { User } from '../user';
  88. import { UserService } from '../user.service';
  89. @Component({
  90. selector: 'app-user',
  91. templateUrl: './user.component.html',
  92. styleUrls: ['./user.component.css']
  93. })
  94. export class UserComponent implements OnInit {
  95.  
  96. users: User[];
  97. constructor(private userService: UserService) { }
  98.  
  99. ngOnInit() {
  100. this.userService.getUsers().subscribe(res => {
  101. this.data = res;
  102. //console.log(this.data);
  103. this.isLoadingResults = false;
  104. }, err => {
  105. console.log(err);
  106. this.isLoadingResults = false;
  107. });
  108. }
  109. getUsers(): void {
  110. this.userService.getUsers().subscribe(
  111. users => this.users = users);
  112. }
  113.  
  114. <table class="table">
  115. <thead>
  116. <tr>
  117. <th>ID</th>
  118. <th>Name</th>
  119. <th>Age</th>
  120. <th>UserName</th>
  121. </tr>
  122. </thead>
  123. <tbody *ngFor="let user of users">
  124. <tr>
  125. <td>{{user.id}}</td>
  126. </a>
  127. <td>{{user.name}}</td>
  128. <td>{{user.age}}</td>
  129. <td>{{user.username}}</td>
  130. </tr>
  131. </tbody>
  132. </table>
Add Comment
Please, Sign In to add comment