K
K
kpanyushin2017-04-09 15:03:03
MySQL
kpanyushin, 2017-04-09 15:03:03

Question on angular node mysql stack?

Hello!
Help, please, to understand. I want to make a CRUD application (the same one, for example) in angular, as a basis, so to speak. I want to make it possible to work with the database (reading, writing, changing, deleting) using these technologies (angular, node + express, mysql). Basically, of course, they are interested in these operations from the side of angular, but some kind of backend is also needed, with which I am currently trying to figure it out)
Suppose there is a RESTful API on nodejs + express, mysql is used as a DBMS. The code is the following:

var app   = require('express')();
var http = require('http').Server(app);
var mysql = require('mysql');
var bodyParser = require("body-parser");
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : '',
    password : '',
    database : 'books',
  });
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
connection.connect();

  
app.get('/',function(req,res){
  var data = {
    "Data":""
  };
  data["Data"] = "Welcome to Book Store DEMO...";
  res.json(data);
});

app.get('/book',function(req,res){
  var data = {
    "error":1,
    "Books":""
  };
  
  connection.query("SELECT * from book",function(err, rows, fields){
    if(rows.length != 0){
      data["error"] = 0;
      data["Books"] = rows;
      res.json(data);
    }else{
      data["Books"] = 'No books Found..';
      res.json(data);
    }
  });
});

app.post('/book',function(req,res){
  var Bookname = req.body.bookname;
  var Authorname = req.body.authorname;
  var Price = req.body.price;
  var data = {
    "error":1,
    "Books":""
  };
  if(!!Bookname && !!Authorname && !!Price){
    connection.query("INSERT INTO book VALUES('',?,?,?)",[Bookname,Authorname,Price],function(err, rows, fields){
      if(!!err){
        data["Books"] = "Error Adding data";
      }else{
        data["error"] = 0;
        data["Books"] = "Book Added Successfully";
      }
      res.json(data);
    });
  }else{
    data["Books"] = "Please provide all required data (i.e : Bookname, Authorname, Price)";
    res.json(data);
  }
});

app.put('/book',function(req,res){
  var Id = req.body.id;
  var Bookname = req.body.bookname;
  var Authorname = req.body.authorname;
  var Price = req.body.price;
  var data = {
    "error":1,
    "Books":""
  };
  if(!!Id && !!Bookname && !!Authorname && !!Price){
    connection.query("UPDATE book SET BookName=?, AuthorName=?, Price=? WHERE id=?",[Bookname,Authorname,Price,Id],function(err, rows, fields){
      if(!!err){
        data["Books"] = "Error Updating data";
      }else{
        data["error"] = 0;
        data["Books"] = "Updated Book Successfully";
      }
      res.json(data);
    });
  }else{
    data["Books"] = "Please provide all required data (i.e : id, Bookname, Authorname, Price)";
    res.json(data);
  }
});

app.delete('/book',function(req,res){
  var Id = req.body.id;
  var data = {
    "error":1,
    "Books":""
  };
  if(!!Id){
    connection.query("DELETE FROM book WHERE id=?",[Id],function(err, rows, fields){
      if(!!err){
        data["Books"] = "Error deleting data";
      }else{
        data["error"] = 0;
        data["Books"] = "Delete Book Successfully";
      }
      res.json(data);
    });
  }else{
    data["Books"] = "Please provide all required data (i.e : id )";
    res.json(data);
  }
});

http.listen(8080,function(){
  console.log("Connected & Listen to port 8080");
});

Everything works in postman, but now how to connect the resulting with angular? For starters, at least just display it in a table (ID, book, author, price). How to open index.html using the code above, what needs to be added? The index.html file will have a table like this:
<table>
    <tr>
        <th>Id</th>
        <th>Book name</th>
        <th>Author name</th>
        <th>Price</th>
    </tr>
    <tr ng-repeat="book in listOfBooks">
        <td>{{book.id}}</td>
        <td>{{book.bookName}}</td>
        <td>{{book.authorName}}</td>
        <td>{{book.price}}</td>
    </tr>
</table>

App.js code that should output data from json file to table
$scope.getBooks = function() {
    	var httpRequest = $http({
    		method: 'GET',
    		url: 'json', // (какой URL должен быть тут, чтобы получить JSON файл который генерирует API?)
    		data: data // что необходимо вставить сюда?
    	}).success(function(data, status) {
    		$scope.listOfBooks = data;
    	});
    };

Am I moving in the right direction at all?
Perhaps there are some backend stubs / emulators and there will be no need to understand all this, but calmly make an application in angular that will allow you to perform crud operations?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question