1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| const express = require('express'); const path = require("path"); const travelnotes_query = require('./query/travelnotes_query'); const app = express(); app.all('*', function (req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); res.header("X-Powered-By", ' 3.2.1') res.header("Content-Type", "application/json;charset=utf-8"); if (req.method == 'OPTIONS') { res.sendStatus(200); } else { next(); } });
app.use(express.urlencoded({ extended: true })); app.use(express.static(path.join(__dirname, 'static')));
app.post('/newtravel', (req, res, next) => { let data = req.body travelnotes_query.addNewTravelData(data, function (rec) { res.json(rec).end(); }); })
var multer = require('multer') var fs = require('fs') const upload = multer({ dest: __dirname + '/static/upload' }) app.post('/newtravel/upload', upload.single('file'), async (req, res) => { let file = req.file; let basePath = __dirname + '/static/upload/'; let oldFileName = file.filename let newFileName = Date.now() + '.png' let filePath = basePath + oldFileName; let newFilePath = basePath + newFileName; if (fs.existsSync(basePath)) { fs.rename(filePath, newFilePath, (err) => { if (err) throw err }) } let imgurl = 'static/upload/' + newFileName let data2 = { imgurl: imgurl } travelnotes_query.addNewTravelImage(data2, function (rec) { res.json(rec).end(); }); })
const port = process.env.PORT || 5000; app.listen(port, function () { console.log(`监听端口: ${port}`) });
const query = require("../dbconn.js");
exports.addNewTravelData = function (data, callback) { console.log('获取的数据:', data); var arr = []; arr.push(data.formtitle); arr.push(data.formconent); arr.push(data.createtime); query('INSERT INTO `travelnotes` (formtitle,formconent,createtime) VALUES (?,?,?)', arr, function (err, results, fields) { if (err) throw err; callback(results); }); }
exports.addNewTravelImage = function (data, callback) { console.log('获取的数据:', data); var arr = []; arr.push(data.imgurl); query('INSERT INTO `travelnotes` (imgurl) VALUES (?)', arr, function (err, results, fields) { if (err) throw err; callback(results); }); }
const mysql = require('mysql2'); const pool = mysql.createPool({ host: '*****', user: '*****', password: '*****', database: '*****', dateStrings: true, waitForConnections: true, connectionLimit: 50, queueLimit: 0 });
var query=function(sql,values,callback){ pool.getConnection(function(err,conn){ if(err){ console.log(err); callback(err,null,null); }else{ conn.query(sql,values,function(qerr,results,fields){ conn.release(); callback(qerr,results,fields); }); } }); }; module.exports=query;
|