Answer the question
In order to leave comments, you need to log in
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");
});
<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>
$scope.getBooks = function() {
var httpRequest = $http({
method: 'GET',
url: 'json', // (какой URL должен быть тут, чтобы получить JSON файл который генерирует API?)
data: data // что необходимо вставить сюда?
}).success(function(data, status) {
$scope.listOfBooks = data;
});
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question