Guest User

server.js

a guest
Jan 10th, 2016
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1.  
  2. var mysql=require('mysql');
  3. var http=require('http');
  4. var async = require('async');
  5. var uuid = require('node-uuid');
  6. var path=require('path');
  7. var express=require('express');
  8. var app=express();
  9. var bodyparser=require('body-parser');
  10. app.use(bodyparser.json());
  11. app.use(bodyparser.urlencoded({extended: true}));
  12. var myconnection = mysql.createConnection({
  13. host : "localhost",
  14.  
  15. user : "root",
  16.  
  17. password : "",
  18.  
  19. database : "siva"
  20. });
  21. app.use(express.static(__dirname + ""));
  22. var uqid= uuid.v1();
  23. // var tt=1;
  24. var status="active";
  25. app.post("/insert",function(req,res){
  26. // console.log(req.body.table.fields | mysum);
  27. /* TODO: Now just check that your drive function is correct, SQL is correct and whether what arguements passed to SQL callback is correct */
  28. myconnection.query('Insert into cate_tbl (cat_id,cat_name,cat_desc,cat_view_count,cat_status) VALUES ("'+uqid+'","'+req.body.cat_name+'","'+req.body.cat_desc+'","'+req.body.tot_count+'","'+status+'")',function(err, results, fields) {
  29. //if (err) throw err;
  30. if (err) {console.log("DB Error"+err); res.send("add failed"+err);}
  31. else
  32. async.each(req.body.fields, function(eachItem, callback) {
  33. console.log(eachItem.result1);
  34. myconnection.query('Insert into cate_item (cat_it_name,cat_pid,cat_it_count,cat_it_desc,address,cat_it_status) VALUES ("'+eachItem.item_name+'","'+uqid+'","'+eachItem.item_count+'","'+eachItem.item_desc+'","'+eachItem.result1+'","'+status+'")',function(err, results, fields) {
  35. if (err) {
  36. console.log("DB Error"+err);
  37. console.log(eachItem.result1);
  38. } else
  39. callback();
  40.  
  41. });
  42. }, function(err, resp) {
  43. if(err) {
  44. console.log("DB error");
  45.  
  46. } else {
  47. res.send("add success");
  48. }
  49. })
  50.  
  51. });
  52. });
  53. app.get('/',function(req,res){
  54. res.sendfile("index.html");
  55. });
  56.  
  57. app.listen(3000,function(){
  58. console.log("It's Started on PORT 3000");
  59. })
Add Comment
Please, Sign In to add comment