nodejs-express解决跨域问题

懒得整理了,下面有数据库的基本操作,跨域问题解决,和接口的响应

const express = require('express');
const app = express();
const moment = require('moment')
const concast = require('./constant')
//导入cors模块,该模块为跨域所用
const cors = require('cors');
app.use(cors());
//解析表单的插件
const bodyParser = require('body-parser');
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
// 端口号
app.listen(5200, () => {
    console.log('正在监听端口5200,http://localhost:5200');
    //192.168.1.114换成你的ip,本机ip查询用cmd=>ipconfig
})

//创建数据库连接对象
const mysql = require('mysql');
const conn = mysql.createConnection({
    host: concast.host, //数据库地址
    user: concast.user, //账号
    password: concast.password, //密码
    database: concast.databases, //库名
    multipleStatements: true //允许执行多条语句
})
app.post("/login", (req, res) => {
    /**
     * 必传
     * id
     * password
     * 教师传type:'T'
     */
    console.log(req.body);
    let data = req.body
    if (req.body.type == 'T') {
        // 教师
        let sql = `select * from teacher where t_id=${data.id} and t_password=${data.password}`
        console.log("执行查询", sql);
        queryLogin(sql, res)
    } else {
        let sql = `select * from student where s_id=${data.id} and s_password=${data.password}`
        console.log("执行查询", sql);
        queryLogin(sql, res)
    }

})

app.post("/register", (req, res) => {
    /**
     * 必传
     * id
     * username
     * password
     * 教师传type:'T'
     */console.log(req.body);
    let data = req.body
    if (req.body.type == 'T') {
        // 教师
        let uname = data.username.toString()
        let pwd = data.password.toString()
        // let sql = `insert into teacher values(${parseInt(data.id)},${uname},${pwd})`
        let sql = 'insert into teacher values('+data.id+',"'+uname+'","'+pwd+'")'
        queryLogin(sql,res)
    }
    else {
        // 学生
        let uname = data.username.toString()
        let pwd = data.password.toString()
        // let sql = `insert into teacher values(${parseInt(data.id)},${uname},${pwd})`
        let sql = 'insert into student values('+data.id+',"'+uname+'","'+pwd+'")'
        queryLogin(sql,res)
    }
})
// 添加成绩
app.post("/addSub",(req,res)=>{
    let data = req.body
    console.log(data);
    // res.json(data)
    let sql = 'insert into fraction values('+data.sid+','+data.tid+',"'+data.subname+'","'+data.subscore+'")'
    queryLogin(sql,res)
})
app.post('/getTeacherData',(req,res)=>{ 
    console.log(req.body);
    let data = req.body
    // res.json({data:"成功"})
    // let sql = "select * from fraction where tid = "+data.id
    let sql = 'select a.*,b.*,c.* from fraction a INNER JOIN student b on a.sid = b.s_id INNER JOIN teacher c on a.tid = c.t_id where a.tid = '+data.id
    queryLogin(sql,res)
})
app.post('/getStudentData',(req,res)=>{ 
    console.log(req.body);
    let data = req.body
    // res.json({data:"成功"})
    // let sql = "select * from fraction where sid = "+data.id
    // let sql = 'select a.*,b.* from fraction a INNER JOIN student b on a.sid = b.s_id where a.sid = '+data.id
    let sql = 'select a.*,b.*,c.* from fraction a INNER JOIN student b on a.sid = b.s_id INNER JOIN teacher c on a.tid = c.t_id where a.sid = '+data.id
    queryLogin(sql,res)
})
// app.post('/getStudentname',(req,res)=>{ 
//     console.log(req.body);
//     let data = req.body
//     // res.json({data:"成功"})
//     let sql = 'select a.*,b.* from fraction a INNER JOIN student b on a.sid = b.s_id where a.tid = '+data.id
//     // let sql = "select * from student where s_id = "+data.id
//     let sql = "select * from student where s_id = "+data.id
//     queryLogin(sql,res)
// })
app.post('/getTeachername',(req,res)=>{ 
    console.log(req.body);
    let data = req.body
    // res.json({data:"成功"})
    let sql = "select * from teacher where t_id = "+data.id
    queryLogin(sql,res)
})
function queryLogin(sql, res) {
    const sqlStr = sql
    conn.query(sqlStr, (err, results) => {
        if (err) {
            console.log("错误日志",err);
            return res.json({ err_code: 1, message: '资料不存在', affextedRows: 0 ,err:err})
        }
        res.json({ err_code: 200, message: results, affextedRows: results.affextedRows })
    })
}
// type === '1' ? 


// item.subType === '6' || item.subType === '7' ? 

// './giftReservation/giftReservation?listId=' + item.listId + '&offerId=' + item.offerId : 

// (item.newStoreFlag?
    
//     '/pages/gift/giftActivity/info/info?listId=' + item.listId + '&offerId=' + item.offerId : 

//     (item.newTimeLimitFlag? '/pages/gift/activityFlame/info/info?listId=' + item.listId + '&offerId=' + item.offerId:  './giftDetail/giftDetail?listId=' + item.listId + '&offerId=' + item.offerId)
    
//     ) 


// : ''


//type === '1' ?item.subType === '6' || item.subType === '7' ? './giftReservation/giftReservation?listId=' + item.listId + '&offerId=' + item.offerId : (item.newStoreFlag?'/pages/gift/giftActivity/info/info?listId=' + item.listId + '&offerId=' + item.offerId :     (item.newTimeLimitFlag? '/pages/gift/activityFlame/info/info?listId=' + item.listId + '&offerId=' + item.offerId:  './giftDetail/giftDetail?listId=' + item.listId + '&offerId=' + item.offerId)): ''
上一篇:【Spark】Spark SQL, DataFrames and Datasets Guide(翻译文,持续更新)


下一篇:python读取DataFrame的某行或某列