0212

Posted cnchengv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了0212相关的知识,希望对你有一定的参考价值。

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

 

以上是关于0212的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数