Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE IF NOT EXISTS 'tasks' (
- 'id' int(11) NOT NULL,
- 'task' varchar(200) NOT NULL,
- 'status' tinyint(1) NOT NULL DEFAULT '1',
- 'created_at' datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
- ALTER TABLE 'tasks' ADD PRIMARY KEY ('id');
- ALTER TABLE 'tasks' MODIFY 'id' int(11) NOT NULL AUTO_INCREMENT;
- INSERT INTO 'tasks' ('id', 'task', 'status', 'created_at') VALUES
- (1, 'Find bugs', 1, '2016-04-10 23:50:40'),
- (2, 'Review code', 1, '2016-04-10 23:50:40'),
- (3, 'Fix bugs', 1, '2016-04-10 23:50:40'),
- (4, 'Refactor Code', 1, '2016-04-10 23:50:40'),
- (5, 'Push to prod', 1, '2016-04-10 23:50:50');
- const express = require('express');
- const mysql = require('mysql');
- const bodyParser = require('body-parser');
- const app = express();
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({
- extended: true
- }));
- // Connect my db
- var connection = mysql.createConnection({
- host : 'localhost',
- user : 'root',
- password : '',
- database : 'dbName'
- });
- connection.connect(function(err) {
- if (err) {
- console.error('error connecting: ' + err.stack);
- return;
- }
- console.log('connected as id ' + connection.threadId);
- });
- app.get('/', function (req, res) {
- res.send('Hello World!');
- });
- // Port that will be listened
- app.listen(3000, function () {
- console.log('Example app listening on port 3000!');
- });
- // the query
- querie = 'SELECT * FROM tasks';
- **THIS IS THE API ENDPOINT FOR THE GET REQUESTS**
- app.get('/todos', function (req, res) {
- connection.query(querie, function (error, results, fields) {
- if (error) throw error;
- return res.send({ error: false, data: results, message: 'Todos list.' });
- });
- });
- <button
- (click)="getData()">GET Profile
- </button>
- import { Component } from '@angular/core';
- import { HttpClient } from "@angular/common/http";
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css']
- })
- export class AppComponent {
- constructor (private httpClient:HttpClient){ }
- getData(){
- this.httpClient.get('/api/todos')
- .subscribe(
- (data:any[]) => {
- console.log(data);
- }
- )
- }
- }
Add Comment
Please, Sign In to add comment