nodejs-express解决跨域问题
2021/10/22 17:11:43
本文主要是介绍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)): ''
这篇关于nodejs-express解决跨域问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16Vue3资料:新手入门必读教程
- 2024-11-16Vue3资料:新手入门全面指南
- 2024-11-16Vue资料:新手入门完全指南
- 2024-11-16Vue项目实战:新手入门指南
- 2024-11-16React Hooks之useEffect案例详解
- 2024-11-16useRef案例详解:React中的useRef使用教程
- 2024-11-16React Hooks之useState案例详解
- 2024-11-16Vue入门指南:从零开始搭建第一个Vue项目
- 2024-11-16Vue3学习:新手入门教程与实践指南
- 2024-11-16Vue3学习:从入门到初级实战教程