node接口(分页返回数据,保存图片)

var express = require('express');
var router = express.Router();

var mysql = require('mysql');
var fs = require('fs');

var conn = mysql.createConnection({
    host: 'sqld.duapp.com',
    user: '40bc378d58254c8a9c33928c78ff8db5',
    password: '80ad82cafdb94b688ea42c29fcc049ee',
    database:'lVKVQJubcBjwVxQRTMnt',
    port: 4050
});
conn.connect();
 
//查询
 /*conn.query('SELECT * from yeba', function(err, result) {
     if (err) throw err;
     //console.log(result);
 })*/
/*
 //新增
 conn.query('insert into yeba (username,password) values("huxiaona","123456")', function(err, result) {
     if (err) throw err;
     console.log(result);
 })
 
//删除
 conn.query('delete from yeba where username="wupeigui"', function(err, result) {
     if (err) throw err;
     console.log(result);
 })
 
conn.query('update user yeba id="1" where username="huxiaona"',function(err,result){
    if (err) {throw err};
    console.log("修改数据成功");
})*/
 
//conn.end();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

//获取
router.get('/get', function(req, res, next) {
  var param = 1;
  if (req.query.page == "") {
        param = 1
    } else{
        param = req.query.page
    }
  var start = (param - 1) * 18;

	conn.query('SELECT * from gaiwang', function(err, result) {
	     if (err){
          var data={"code":404,"data":''}
			     res.send(JSON.stringify(data));
	    	}else{
          var allCount=result.length;
          var allPage = parseInt(allCount)/18;
          var pagination={
            allCount:allCount,
            allPage:allPage,
            page:param
          }
          var shuju=[];
          for(var i=start;i<(start+18);i++){
            var liju={
              id:result[i].id,
              title:result[i].title,
              url:result[i].img,
              price:result[i].prie
            };
            shuju.push(liju);
          };
          //res.jsonp(shuju);
          var data={"code":200,"meta":pagination,"data":shuju}
          res.send(JSON.stringify(data));
        };
	       
	 })
  //res.render('index', { title: 'Express' });
});

//添加
router.post('/post', function(req, res, next) {
  //res.render('index', { title: 'Express' });
  if(req.body.goods_name!=undefined||req.body.goods_logo!=undefined||req.body.goods_price!=undefined){
      var _title=req.body.goods_name
      var _imgData=req.body.goods_logo
      var _prie=req.body.goods_price

      var time = Date.parse(new Date());
      var _name="./public/updown/"+time+".jpg"
      var _images="/updown/"+time+".jpg"

      var base64Data = _imgData.replace(/^data:image\/\w+;base64,/, "");
      var dataBuffer = new Buffer(base64Data, 'base64');

      fs.writeFile(_name, dataBuffer, function(err) {
          if(err){
            var data={"code":404,"msg":"图片上传失败"}
            res.send(JSON.stringify(data));
          }else{
            conn.query('insert into gaiwang (title,img,prie) values("'+_title+'","'+_images+'","'+_prie+'")', function(err, result) {
                 if (err){
                  var data={"code":404,"msg":"增加失败"}
                  res.send(JSON.stringify(data));
                 }else{
                  var data={"code":200,"msg":"增加成功","sql":mysql}
                  res.send(JSON.stringify(data));
                 };
             })
          }
      });

          
  }else{
    var data={"code":404,"msg":"必填数据为空"}
    res.send(JSON.stringify(data));
  }
});

//删除
router.post('/delete', function(req, res, next) {
  var _id=req.body.id
 conn.query('delete from gaiwang where id="'+_id+'"', function(err, result) {
     if (err){
        var data={"code":404,"msg":"删除失败"}
        res.send(JSON.stringify(data));
     }else{
        var data={"code":200,"msg":"删除成功"}
        res.send(JSON.stringify(data));
     }
 })
});


router.get('/put', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;

  

上传到七牛

node插件var qiniu = require('qiniu');

var accessKey = 'N9Po-wYE118QUCQByCE_cNywF_gPdU9XqeBIkakO';
	var secretKey = 'BARYJdb8Y40jXZdRMPYNPNIYmbEFcPjmOlRF8ynE';
	var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);

	var bucket='lylcc'
	var options = {
	  scope: bucket,
	};
	var putPolicy = new qiniu.rs.PutPolicy(options);
	var uploadToken=putPolicy.uploadToken(mac);
	console.log(uploadToken)

	var config = new qiniu.conf.Config();
	config.zone = qiniu.zone.Zone_z2;

	var file64=req.body.txt
	var time = Date.parse(new Date());
	var _name="./public/updown/"+time+".jpg"
	var _images="/updown/"+time+".jpg"

	var base64Data = file64.replace(/^data:image\/\w+;base64,/, "");
	var dataBuffer = new Buffer(base64Data, 'base64');

	fs.writeFile(_name, dataBuffer, function(err) {
	  if(err){
	    var data={"code":401,"msg":"图片上传失败"}
	    console.log(data)
	    //res.send(JSON.stringify(data));
	  }else{

	    var formUploader = new qiniu.form_up.FormUploader(config);
		var putExtra = new qiniu.form_up.PutExtra();
		var key=time+'.png';
		// 文件上传
		formUploader.putFile(uploadToken, key, _name, putExtra, function(respErr,
		  respBody, respInfo) {
		  if (respErr) {
		    throw respErr;
		  }
		  if (respInfo.statusCode == 200) {
		    //console.log(respBody);
		  } else {
		    console.log(respInfo.statusCode);
		    //console.log(respBody);
		  }
		  res.send(respBody);
		})
	  }
	});

  

posted @ 2017-12-29 15:34  fm060  阅读(969)  评论(0编辑  收藏  举报