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-12-23【JS逆向百例】爱疯官网登录逆向分析
- 2024-12-21Vue3教程:新手入门到实践应用
- 2024-12-21VueRouter4教程:从入门到实践
- 2024-12-20Vue3项目实战:从入门到上手
- 2024-12-20Vue3项目实战:新手入门教程
- 2024-12-20VueRouter4项目实战:新手入门教程
- 2024-12-20如何实现JDBC和jsp的关系?-icode9专业技术文章分享
- 2024-12-20Vue项目中实现TagsView标签栏导航的简单教程
- 2024-12-20Vue3入门教程:从零开始搭建你的第一个Vue3项目
- 2024-12-20从零开始学习vueRouter4:基础教程