var express = require('express')
var app = express()
var fs = require('fs')
var mysql = require('mysql')
var cors = require('cors')
// 设置跨域访问
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')
next()
})
app.use(
cors({
origin: ['http://localhost:3000'],
methods: ['get', 'post'],
allowedHeaders: ['Content-Type', 'Authorization']
})
)
app.get('/list', function (req, res) {
fs.readFile(__dirname + '/' + 'list.json', 'utf-8', function (err, data) {
console.log('执行服务成功')
res.end(data)
})
})
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '*********',
port: '3306',
database: 'tables'
})
db.connect(err => {
if (err) throw err
console.log('连接成功')
})
// 判断是否有newsjk或newztk
app.get('/getDatabases', (req, res) => {
let sql = 'show tables;'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/newsjk', (req, res) => {
let sql = 'SELECT * FROM newsjk'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/newsjkCount', (req, res) => {
let sql = 'SELECT * FROM newsjk_count'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/newztk', (req, res) => {
let sql = 'SELECT *FROM newztk'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/newztkCount', (req, res) => {
let sql = 'SELECT *FROM newztk_count'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/ddsjk', (req, res) => {
let sql = 'SELECT * FROM ddsjk'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/ddsjkCount', (req, res) => {
let sql = 'SELECT * FROM ddsjk_count'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/ddztk', (req, res) => {
let sql = 'SELECT *FROM ddztk'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/ddztkCount', (req, res) => {
let sql = 'SELECT *FROM ddztk_count'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/ndsjk', (req, res) => {
let sql = 'SELECT *FROM ndsjk'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/ndsjkCount', (req, res) => {
let sql = 'SELECT *FROM ndsjk_count'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/ndztk', (req, res) => {
let sql = 'SELECT *FROM ndztk'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/ndztkCount', (req, res) => {
let sql = 'SELECT *FROM ndztk_count'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/nsdsjk', (req, res) => {
let sql = 'SELECT *FROM nsdsjk'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/nsdsjkCount', (req, res) => {
let sql = 'SELECT *FROM nsdsjk_count'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/nsdztk', (req, res) => {
let sql = 'SELECT *FROM nsdztk'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/nsdztkCount', (req, res) => {
let sql = 'SELECT *FROM nsdztk_count'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/school', (req, res) => {
let sql = 'SELECT *FROM school'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/schoolpm', (req, res) => {
let sql = 'SELECT *FROM schoolpm'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/schoolLw', (req, res) => {
let sql = 'SELECT *FROM schoolLw'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/hospital', (req, res) => {
let sql = 'SELECT *FROM hospital'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/hospitalpm', (req, res) => {
let sql = 'SELECT *FROM hospitalpm'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/hospitalLw', (req, res) => {
let sql = 'SELECT *FROM hospitallw'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/hospitalTable', (req, res) => {
let sql = 'select hospital1.year,hospital2.year,hospital3.year,hospital4.year,hospital1.value,hospital2.value,hospital3.value,hospital4.value from ((hospital1 left join hospital2 on hospital1.name=hospital2.name) left join hospital3 on hospital1.name=hospital3.name) left join hospital4 on hospital1.name=hospital4.name'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/dangzheng', (req, res) => {
let sql = 'SELECT *FROM dangzheng'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/dangzhengLw', (req, res) => {
let sql = 'SELECT *FROM dangzhenglw'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/tongjiCount', (req, res) => {
let sql = 'SELECT *FROM tongji_count'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/tongjiFw', (req, res) => {
let sql = 'SELECT *FROM tongji_fawen'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
app.get('/tongjiBy', (req, res) => {
let sql = 'SELECT *FROM tongji_beiyin'
db.query(sql, (err, result) => {
if (err) {
console.log(err)
} else {
console.log(result)
res.json(result)
}
})
})
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log(host, port)
})