var express = require(‘express‘); var router = express.Router(); var mysql = require(‘mysql‘); var fs = require(‘fs‘); var multer = require(‘multer‘); var upload = multer({dest: ‘upload_tmp/‘}); function myCreateCon(){ var connection = mysql.createConnection({ host: ‘10.46.52.205‘, user: ‘zxdss‘, password: ‘zxdss‘, database:‘test‘ }); return connection; } router.post(‘/upload/:id‘, upload.any(), function(req, res, next) { console.log(req.files[0]); // 上传的文件信息 console.log("room id--"+req.params.id); var roomid = req.params.id; var filename = req.files[0].originalname; var des_file = "./" + req.files[0].originalname; var queryStr=‘update room set image1="‘+filename+‘" where id=‘+roomid; console.log(‘ queryStr=‘+queryStr); fs.readFile( req.files[0].path, function (err, data) { fs.writeFile(des_file, data, function (err) { if( err ){ console.log( err ); } else{ var connection=myCreateCon(); connection.connect(); connection.query(queryStr, function(err, rows, fields) { if (err) throw err; console.log(‘查询结果为: ‘, rows); //res.send(rows); response = { message:‘File uploaded successfully‘, filename:req.files[0].originalname }; console.log( response ); res.end( JSON.stringify( response ) ); }); connection.end(); } }); }); }); /* GET rooms listing. */ router.get(‘/all‘, function(req, res, next) { var connection=myCreateCon(); connection.connect(); connection.query(‘select * from room‘, function(err, rows, fields) { if (err) throw err; console.log(‘查询结果为: ‘, rows); res.send(rows); }); connection.end(); }); //get id router.get(‘/:id‘, function(req, res, next) { console.log("--"+req.params.id); var roomid = req.params.id; var connection=myCreateCon(); connection.connect(); connection.query(‘select * from room where id = ‘+roomid, function(err, rows, fields) { if (err) throw err; console.log(‘查询结果为: ‘, rows); res.send(rows); }); connection.end(); }); module.exports = router;
/api/room/upload/41