当前位置: 首页 > news >正文

项目内置Express服务器解决Axios发送POST请求mock数据报404错误问题[Vue.js项目实践: 新冠自检系统]

Logo

新冠疫情自我检测系统网页设计开发文档

Sylvan Ding 的第一个基于 Vue.js 的项目. 本项目所提供的信息,只供参考之用,不保证信息的准确性、有效性、及时性和完整性,更多内容请查看国家卫健委网站!
Explore the docs »

View Demo · Report Bug · Request Feature

homepage

项目内置Express服务器解决Axios发送POST请求mock数据报404错误问题

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

When axios used ‘POST’ method to request mock data in /static/mock, it would throw a 404 error, indicating that the JSON file was not found. We, however, are able to do so using ‘GET’ method. It is a problem that the server can’t response correctly, instead of an issue of axios. In our project, we build a simple web server with Express to deal with the ‘POST’ 404 error. Note that the server will only work under development environment.

开发环境

node14.16.1
npm8.18.0
vue-cli2.9.6
vue2.5.2

解决方案

Router

# Installation
npm i express morgan -D --legacy-peer-deps

morgan is a HTTP request logger middleware for node.js.

To follow the standard, CommonJS, we create a module named dev.server.js in config/ as our Express configuration and startup file:

// config/dev.server.js

const path = require('path')
const logger = require('morgan')
const express = require('express')
const feedback_success = require('../static/mock/feedback_success.json')
const feedback_error = require('../static/mock/feedback_error.json')

function run(port) {
  const app = express()
  const router = express.Router()

  // every request gets logged
  router.use(logger())

  // POST method route
  router.post('/mock/feedback_success.json', function(req, res) {
    // send a JSON response
    res.json(feedback_success)
  })
  router.post('/mock/feedback_error.json', function(req, res) {
    res.json(feedback_error)
  })

  // express.static serves all static files
  router.use('/', express.static(path.resolve(__dirname, '../static')))

  // load the router module in the app
  app.use('/static', router)

  // start app
  app.listen(port)
}

module.exports = { run }

The above Express app configuration allows you to access any static resources in /static by ‘GET’ method, meanwhile specific resources such as /mock/feedback_success.json can also be gotten by ‘POST’.

Port Setting

You are allowed to appoint your Express server port in config/index.js:

// config/index.js

const port = 8080
const devServerPort = 8081

module.exports = {
  dev: {
    // ...
    // Various Dev Server settings
    host: 'localhost', // can be overwritten by process.env.HOST
    port, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    devServerPort // Express web server port
    // ...
  }
  // ...
}

We also set global process.env variable DEVSERVERPORT, so that you can reference devServerPort anywhere in the project.

// build/webpack.dev.conf.js

//...
module.exports = new Promise((resolve, reject) => {
  portfinder.basePort = process.env.PORT || config.dev.port
  portfinder.getPort((err, port) => {
    if (err) {
      reject(err)
    } else {
      // publish the new Port, necessary for e2e tests
      process.env.PORT = port
      process.env.DEVSERVERPORT = config.dev.devServerPort // !!!
      // add port to devServer config
      devWebpackConfig.devServer.port = port
      // ...

Usage

Import dev.server.js in config/dev.env.js and run the server:

// config/dev.env.js

const devServer = require('./dev.server')
const port = require('./index').dev.devServerPort

// ...

devServer.run(port)

For example, when sending ‘POST’ to http://localhost:devServerPort/static/mock/feedback_success.json, you will get the return { "res": "1", "msg": "反馈提交成功!" }.

转载请注明出处:©️ Sylvan Ding 2022

相关文章:

  • Pytorch搭建自定义神经网络
  • Python数据分析——基础数据结构
  • TestNG-常用注解介绍
  • STM32时钟系统和TIMER配置(溢出中断/PWM)实例
  • 随想录一刷Day04——链表
  • 【javaweb简单教程】2.JSP实现数据传递和保存(含四大作用域及简单示例)
  • 7.ROS2笔记-节点
  • 【C++】类和对象(下篇)(万字)
  • 【牛客 - 剑指offer】JZ67 把字符串转换成整数 Java实现
  • python采集火热弹幕数据并做词云图可视化分析
  • 【小程序从0到1】模版与配置|数据绑定|事件绑定
  • NetSuite SuiteQL Query Tool
  • 功能异常强大,推荐这款 Python 时序异常检测神器
  • 串的存储结构 --王道
  • React路由规则的定义、声明式导航、编程式导航
  • 2017-09-12 前端日报
  • Angular 4.x 动态创建组件
  • Java编程基础24——递归练习
  • pdf文件如何在线转换为jpg图片
  • Python_OOP
  • Python十分钟制作属于你自己的个性logo
  • Redis字符串类型内部编码剖析
  • spring学习第二天
  • SSH 免密登录
  • vue-router的history模式发布配置
  • 仿天猫超市收藏抛物线动画工具库
  • 使用 5W1H 写出高可读的 Git Commit Message
  • 使用common-codec进行md5加密
  • 推荐一个React的管理后台框架
  • 学习笔记DL002:AI、机器学习、表示学习、深度学习,第一次大衰退
  • 异步
  • 字符串匹配基础上
  • 阿里云服务器购买完整流程
  • 容器镜像
  • ​HTTP与HTTPS:网络通信的安全卫士
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • %check_box% in rails :coditions={:has_many , :through}
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (九)信息融合方式简介
  • (四)Tiki-taka算法(TTA)求解无人机三维路径规划研究(MATLAB)
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • (转)用.Net的File控件上传文件的解决方案
  • (轉貼) UML中文FAQ (OO) (UML)
  • .sys文件乱码_python vscode输出乱码
  • /usr/lib/mysql/plugin权限_给数据库增加密码策略遇到的权限问题
  • [ JavaScript ] JSON方法
  • [ vulhub漏洞复现篇 ] ECShop 2.x / 3.x SQL注入/远程执行代码漏洞 xianzhi-2017-02-82239600
  • [AI]ChatGPT4 与 ChatGPT3.5 区别有多大
  • [bzoj 3534][Sdoi2014] 重建
  • [C++]C++类基本语法
  • [GDOUCTF 2023]<ez_ze> SSTI 过滤数字 大括号{等
  • [Grafana]ES数据源Alert告警发送
  • [javaSE] GUI(事件监听机制)
  • [Machine Learning][Part 7]神经网络的基本组成结构
  • [office] 在Excel2010中设定某些单元格数据不参与排序的方法介绍 #其他#知识分享#笔记