· 7 years ago · Sep 25, 2018, 04:14 AM
1CREATE TABLE IF NOT EXISTS 'tasks' (
2
3 'id' int(11) NOT NULL,
4 'task' varchar(200) NOT NULL,
5 'status' tinyint(1) NOT NULL DEFAULT '1',
6 'created_at' datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
7) ENGINE=InnoDB DEFAULT CHARSET=latin1;
8
9
10
11ALTER TABLE 'tasks' ADD PRIMARY KEY ('id');
12ALTER TABLE 'tasks' MODIFY 'id' int(11) NOT NULL AUTO_INCREMENT;
13
14INSERT INTO 'tasks' ('id', 'task', 'status', 'created_at') VALUES
15(1, 'Find bugs', 1, '2016-04-10 23:50:40'),
16(2, 'Review code', 1, '2016-04-10 23:50:40'),
17(3, 'Fix bugs', 1, '2016-04-10 23:50:40'),
18(4, 'Refactor Code', 1, '2016-04-10 23:50:40'),
19(5, 'Push to prod', 1, '2016-04-10 23:50:50');
20
21const express = require('express');
22 const mysql = require('mysql');
23 const bodyParser = require('body-parser');
24 const app = express();
25
26 app.use(bodyParser.json());
27 app.use(bodyParser.urlencoded({
28 extended: true
29 }));
30
31 // Connect my db
32 var connection = mysql.createConnection({
33 host : 'localhost',
34 user : 'root',
35 password : '',
36 database : 'dbName'
37 });
38
39 connection.connect(function(err) {
40 if (err) {
41 console.error('error connecting: ' + err.stack);
42 return;
43 }
44
45 console.log('connected as id ' + connection.threadId);
46 });
47
48 app.get('/', function (req, res) {
49 res.send('Hello World!');
50 });
51
52 // Port that will be listened
53 app.listen(3000, function () {
54 console.log('Example app listening on port 3000!');
55 });
56
57 // the query
58 querie = 'SELECT * FROM tasks';
59
60**THIS IS THE API ENDPOINT FOR THE GET REQUESTS**
61
62 app.get('/todos', function (req, res) {
63 connection.query(querie, function (error, results, fields) {
64 if (error) throw error;
65 return res.send({ error: false, data: results, message: 'Todos list.' });
66 });
67 });
68
69<button
70 (click)="getData()">GET Profile
71</button>
72
73import { Component } from '@angular/core';
74import { HttpClient } from "@angular/common/http";
75
76@Component({
77 selector: 'app-root',
78 templateUrl: './app.component.html',
79 styleUrls: ['./app.component.css']
80})
81export class AppComponent {
82 constructor (private httpClient:HttpClient){ }
83
84 getData(){
85 this.httpClient.get('/api/todos')
86 .subscribe(
87 (data:any[]) => {
88 console.log(data);
89 }
90 )
91 }
92}