nodejs express编写接口,基于cors解决接口跨域问题
taotaoit nodejs 2023-01-16 743 0
关于本站

“最难不过坚持”

本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作

有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339

6475891 2614 39
最新评论
https://jueru.net/
评 留言
:weixiao:
评 留言
:shuijiao: :weiqu: :zhenbang: :leng:
评 留言
:yiwen: :yiwen: :yiwen: :yiwen:
评 EasySass: could not generate CSS file. See Output panel for details.
这个业务逻辑多少都有点奇怪了,阅读浏览次数增值在新闻详情页的控制器方法里setInc,这怎么还写进模型事件里了。如果非要用onAfterRead也可以,把新闻文章的内容单独分出来一个news_content表,然后把它和news做关联,然后给news_content表的onAfterRead事件做增值处理,这样点进新闻页内查询到文章内容时才会触发它。
评 TP6模型事件-查询后onAfterRead不好用
文章标签更多
ThinkPHP (254)
Mysql (58)
DedeCms (33)
jQuery (67)
证件照 (1)
setInc (4)
setDec (4)
onclick (5)
打开边栏(ESC) 关闭边栏(ESC)

1,创建服务器页面代码index.js

// 导入 express 模块
const express = require('express')
// 创建 express 的服务器实例
const app = express()

// 如果要获取 URL-encoded 格式的请求体数据,必须配置中间件-post请求时用到
app.use(express.urlencoded({ extended: false }))

// app.js [导入并注册路由模块]
const apiRouter = require('./apiRouter.js')
app.use('/api',apiRouter)

// 调用app.listen() 方法,指定端口号并启动web服务器
app.listen(80,function(){
  console.log('Express server running at http://127.0.0.1')
})
2,express路由,编写get接口,post接口apiRouter.js
// 路由模块
const express = require('express')
const apiRouter = express.Router()

// get路由
apiRouter.get('/get', (req, res) => {
  // 1.获取到客户端通过查询字符串,发送到服务器的数据
  const query = req.query
  // 2.调用res.send()方法,把数据响应给客户端
  res.send({
      status: 0,              //状态,0表示成功,1 表示 失败
      msg: 'GET请求成功!',    // 状态描述
      data: query            // 需要响应给客户端的具体数据
  })
})
  

// post路由
apiRouter.post('/post', (req, res) => {
  // 获取客户端通过请求体,发送到服务器的  URL-encoded 数据
  const body = req.body
  // 2. 调用 res.send() 方法,把数据响应给客户端
  res.send({
    status: 0,              // 状态,0 表示成功,1 表示失败
    msg: "POST请求成功",    // 状态描述
    data: body              // 需要响应给客户端的具体数据
  })
})
  

module.exports = apiRouter
3,命令窗口中执行命令,node index.js

然后就可以在apiPost工具中,请求接口测试数据了

from clipboard

4,基于cors解决接口跨域问题

4.1通过jquery编辑ajax请求页面index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
  </script>
</head>
<body>
  <button id="btnGET">get请求</button>
  <button id="btnPOST">post请求</button>
  <script>
    $(function(){
      // 测试GET接口
      $('#btnGET').on('click',function(){
        $.ajax({
          type:'GET',
          url:'http://127.0.0.1/api/get',
          data:{id:1,name:'zs'},
          success:function(res){
            console.log(res);
          }
        })
      })
      // 测试POST接口
      $('#btnPOST').on('click',function(){
        $.ajax({
          type:'POST',
          url:'http://127.0.0.1/api/post',
          data:{id:1,name:'zs'},
          success:function(res){
            console.log(res);
          }
        })
      })
    })
  </script>
</body>
</html>
4.2点击请求按钮,返回

from clipboard

说明有跨域问题

4.3 cors是express的一个第三方中间件,通过安装和配置cors中间件,可以很方便解决跨域问题,使用步骤分3步:
1,运行npm i cors安装中间件
2,使用const cors = require('cors')导入中间件
3,在路由之前调用app.use(cors())配置中间件

完整代码如下:

// 导入 express 模块
const express = require('express')
// 创建 express 的服务器实例
const app = express()

// 如果要获取 URL-encoded 格式的请求体数据,必须配置中间件
app.use(express.urlencoded({ extended: false }))

// 一定要在路由之前,配置cors这个中间件,从而解决接口跨域的问题
const cors = require('cors')
app.use(cors())

// app.js [导入并注册路由模块]
const apiRouter = require('./apiRouter.js')
app.use('/api',apiRouter)

// 调用app.listen() 方法,指定端口号并启动web服务器
app.listen(80,function(){
  console.log('Express server running at http://127.0.0.1')
})

版权声明:本站原创文章,允许自由转载。

相关推荐
windows如何把已安装的nodejs高版本降级为低版本
nodejs | 2022-10-17 2082
第一步:先清空本地安装的node.js版本 1.按健win+R弹出窗口,键盘输入cmd,然后敲回车,然后进入命令控制行窗口,并输入where node查看之前本地安装的node的路径 2.找到上面找到的路径,将node.exe所在的父目录里面的所有东西都删除 3.为了彻底删除之前安装的node.js,鼠...
This application is only supported on Windows 8.1, Windows Server 2012 R2, or hi gher.
nodejs | 2022-07-06 1802
有的nodejs版本不支持win7,在win7系统中执行npm -v时会有以下提示 This application is only supported on Windows 8.1, Windows Server 2012 R2, or hi gher. 下载v12.16.2及之前的版本即可 https://nodejs.org/d...
评论:0条
评论加载中...
发表评论