Advertisement
Guest User

Untitled

a guest
Sep 6th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. var mysql = require('mysql');
  2. var express = require('express');
  3.  
  4. var app= express();
  5.  
  6. var bodyParser = require('body-parser');
  7.  
  8. app.use (bodyParser.json());
  9. app.use (bodyParser.urlencoded({ extended: true }));
  10.  
  11.  
  12.  
  13. var server = app.listen(process.env.PORT||8000, function(err){
  14. if (err) throw err;
  15. console.log('Connected to port 8000');
  16. });
  17.  
  18. var conn = mysql.createConnection({
  19. host: 'localhost',
  20. user: 'root',
  21. password: 'rootpass',
  22. database: 'mydb'
  23. });
  24.  
  25. conn.connect(function(err){
  26. if(err) throw err;
  27. console.log ('Connected');
  28. });
  29.  
  30. conn.query('CREATE TABLE IF NOT EXISTS student\
  31. (\
  32. id INT,\
  33. nume VARCHAR(30),\
  34. prenume VARCHAR(30),\
  35. facultate VARCHAR(30),\
  36. oras VARCHAR(30)\
  37. )', function(err, res){
  38. if (err) throw err;
  39. console.log(res);
  40. });
  41.  
  42. app.get('/insert.html', function(req, res){
  43. res.sendFile(__dirname + '/insert.html');
  44. });
  45.  
  46. app.post('/insert', function(req, res){
  47. var arr = [[req.body.id, req.body.nume, req.body.prenume, req.body.facultate, req.body.oras]];
  48.  
  49. conn.query('INSERT INTO student VALUES ?', [arr], function(err, result){
  50. if(err) throw err;
  51. console.log('succes');
  52. });
  53. res.end();
  54. });
  55.  
  56. conn.query('SELECT * FROM student', function(err, res){
  57. if(err) throw err;
  58. console.log(res);
  59. });
  60.  
  61. app.get('/select',function(req, res) {
  62. conn.query ('SELECT * FROM student', function(err, result) {
  63. if(err) throw err;
  64. res.send(result);
  65. });
  66. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement