Advertisement
Guest User

Untitled

a guest
Mar 11th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. var express = require("express");
  2. var app = express();
  3. var router = express.Router();
  4. var path = __dirname + '/views/';
  5. var http = require('http');
  6. var mysql = require('mysql');
  7. var bodyParser = require("body-parser");
  8.  
  9. app.use(bodyParser.urlencoded({ extended: true }));
  10. app.use(bodyParser.json());
  11.  
  12. app.disable('x-powered-by');
  13.  
  14. var connection = mysql.createConnection({
  15. host: 'localhost',
  16. user: 'root',
  17. password: 'xxx',
  18. database: 'xxx',
  19. });
  20.  
  21. router.use(function (req,res,next) {
  22. console.log("/" + req.method);
  23. next();
  24. });
  25.  
  26. router.get("/",function(req,res){
  27. res.sendFile(path + "index.html");
  28. });
  29. var queryString = 'SELECT * FROM bets';
  30.  
  31.  
  32. router.get("/bets",function(req, res){
  33. showBets(res);
  34. res.sendFile(path + "bets.html");
  35. console.log('closing');
  36. res.end();
  37. });
  38.  
  39. function showBets(res1){
  40. connection.query(queryString, function(err, rows, fields, res2) {
  41. if (err) throw err;
  42.  
  43. else{
  44. for (var i in rows) {
  45. res1.write(rows[i].title);
  46. console.log(rows[i].creator);
  47. }
  48. }
  49. });
  50. }
  51.  
  52. app.use("/",router);//säger till express att använda routes vi har definierat
  53.  
  54. app.post('/data', function(req, res){
  55. var title = req.body.titleinput;
  56. var description = req.body.descriptioninput;
  57. var password = req.body.passwordinput;
  58. var creator = req.body.creatorinput;
  59. var hours = req.body.hoursinput;
  60.  
  61. var post = {
  62. description : description,
  63. title : title,
  64. password : password,
  65. creator : creator,
  66. hours : hours
  67.  
  68. };
  69. console.log(post);
  70. var query = connection.query('INSERT INTO bets SET ?', post, function(err, result) {
  71. if (err) {
  72. res.send("There was a problem adding the information to the database.");
  73. }
  74. console.log(query.sql);
  75.  
  76. });
  77. });
  78. http.createServer(app).listen(8080, function(){
  79. console.log('Node server is running');
  80. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement