Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- var app = express();
- var bodyParser = require('body-parser');
- app.use(express.static('./'));
- app.use(bodyParser.urlencoded({ extended: true }));
- var mysql = require('mysql');
- var connection = mysql.createConnection({
- host : ' ',
- user: 'root',
- password: '',
- database: 'test'
- });
- connection.connect();
- app.get('/',(req,res) => {
- res.send("Hello try at /books");
- });
- app.get('/books',function(req,res){
- var {keyword} =req.query;
- connection.query('SELECT * FROM books WHERE title LIKE ?','%'+keyword+'%', function (error, results, fields) {
- if (error){
- throw error;
- alert("Book not found");
- }
- for(let result of results){
- console.log(result);
- }
- res.send(results);
- });
- });
- app.post('/books',function(req,res){
- var title = req.body.title;
- var author = req.body.author;
- var genre = req.body.genre;
- var price = req.body.price;
- connection.query('INSERT INTO books (author,title,genre,price) VALUES (?, ?, ?, ?)', [author, title, genre, parseFloat(price)],function(error,results,fields){
- if(error){
- throw error;
- alert("Could not add book, check if price is number")
- }else{
- res.send("book added");
- }
- });
- });
- app.listen(3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement