Guest User

Untitled

a guest
Aug 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. const mysql = require('mysql')
  2. const express = require('express')
  3.  
  4. const app = express()
  5.  
  6. app.use(express.json())
  7. app.use(express.urlencoded({ extended: false }))
  8.  
  9. // Connection database
  10. const connection = mysql.createConnection({
  11. host: '127.0.0.1',
  12. user: 'root',
  13. password: 'root',
  14. database: 'mydb'
  15. })
  16.  
  17.  
  18. // Routing
  19. const router = express.Router();
  20.  
  21. // GET /
  22. router.get('/', (req, res) => {
  23. return res.json({
  24. message: mensaje
  25. })
  26. })
  27.  
  28. connection.connect()
  29.  
  30. router.get('/artist', (req, res) => {
  31. const whereCondition = (req.query.id) ? ` WHERE idartistas = ${req.query.id}` : ''
  32. connection.query(`SELECT * FROM artistas ${whereCondition}`, (err, results, fields) => {
  33. if (err) return res.json({ error: err })
  34. return res.json({ data: results })
  35. })
  36. })
  37.  
  38. router.post('/artist', (req, res) => {
  39. console.log(req)
  40. connection.query(`
  41. INSERT INTO artistas(nombre, debut, valoracion, bio)
  42. VALUES(
  43. "${req.body.nombre}",
  44. "${req.body.debut}",
  45. ${req.body.valoracion},
  46. "${req.body.bio}"
  47. )
  48. `, (err, results, fields) => {
  49. if (err) return res.json({ error: err })
  50. return res.json({
  51. message: 'Artist created!'
  52. });
  53. })
  54. });
  55.  
  56. app.use('/', router);
  57. app.listen('8080');
  58. console.log('Listening on port 8080')
Add Comment
Please, Sign In to add comment