express+ mysql搭建服务器

express框架

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var express = require("express");
var app = express();
 
var bodyParser = require("body-parser");
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
  extended: false
}));
 
app.get("/searchTab", function (req, res) {
  const {name,date1,date2} = req.query;
···逻辑  需要拿到数据库res.send("写回去的内容")
}
 
app.post("/postTab", function (req, res) {
  const {name,date1,date2} = req.body;
···逻辑  需要拿到数据库res.send("写回去的内容")
}

连接数据库

服务器是node程序,前端是另一个程序,所以

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const mysql = require('mysql');
 
//数据库
var pool = mysql.createPool({
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'test',
  port: 3306,
  multipleStatements: true,
  charset: "UTF8_GENERAL_CI"
});
//跨域
var cors = require("cors");
app.use(cors())
 
 pool.query(str, function (err, rows) {
    if (err) {
      res.send("0")
      return
    } else {}});  

上传文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var multer = require("multer");
var path = require("path");
 
app.use(express.static(path.join(__dirname, 'public/img')))
//保存在的位置
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'public/img');
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + "-" + file.originalname);
  }
})
 
var upload = multer({
  storage: storage
})
app.post('/postImage', upload.single("file"), function (req, res, next) {
  //res.json(req.file.originalname);
  // 服务器IP
  const url = "http://localhost:8089/" + req.file.filename
  res.send(url);
  next()
});

参考文章:https://segmentfault.com/a/1190000011740828 

momentjs:https://momentjs.com/docs/#/get-set/

 

 

posted @   lxq3280  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结
点击右上角即可分享
微信分享提示