当前位置:网站首页>Nodejs uses the express framework to post the request message "badrequesterror:request aborted"
Nodejs uses the express framework to post the request message "badrequesterror:request aborted"
2022-07-24 13:56:00 【Senzhiqianshou】
Report the wrong diagram first :

Let's go straight to the results , It depends on whether you have enabled mock.js of .
Because my front-end framework adopts VueElementAdmin, This framework has mockJS Simulation data of . So I also use this configuration by default .
The front end uses axios As a request framework , The key codes are as follows :
import axios from 'axios'
// establish axios example
const service = axios.create({
baseURL: 'http://127.0.0.1:95', // url = base url + request url
withCredentials: true, // send cookies when cross-domain requests
timeout: 5000 // request timeout
})
export default service
The main back-end code is as follows :
const express = require('express')
const server = express()
const startServer = () => {
server.use(express.json())
server.use(express.urlencoded({ extended: true }))
server.post('/home/login', (req, resp) => {
console.log(req.query)
resp.send({
code: 200
})
})
const { port, hostName } = $serverConfig
server.listen(port, hostName, () => {
console.log(` Service started :http://${ hostName }:${ port }`)
})
}in addition , Because we need to solve cross domain problems , The front end uses a proxy :
'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')
function resolve(dir) {
return path.join(__dirname, dir)
}
const name = defaultSettings.title || 'vue Admin Template' // page title
const port = process.env.VUE_APP_PORT
const baseApi = process.env.VUE_APP_BASE_API
const targetApi = process.env.VUE_APP_TARGET_API
const pathRewrite = process.env.VUE_APP_REWRITE_PATH
module.exports = {
publicPath: '/',
outputDir: 'dist',
assetsDir: 'static',
lintOnSave: process.env.NODE_ENV !== 'production',
productionSourceMap: false,
devServer: {
port: port,
open: true,
overlay: {
warnings: false,
errors: true
},
proxy: {
[baseApi]: {
target: targetApi,
changeOrigin: true,
pathRewrite: {
['^' + baseApi]: pathRewrite
}
}
},
before: require('./mock/mock-server.js')
}
}
# just a flag
ENV = 'development'
VUE_APP_ENV='development'
# base api
VUE_APP_BASE_API = '/dev-api'
VUE_APP_REWRITE_PATH = ''
# Page service port
VUE_APP_PORT = 4310
# Destination forwarding address
VUE_APP_TARGET_API='http://127.0.0.1:95'
analysis
axios default header Inside content-type Namely application/json. Then the back-end parsing is also json form . But I used this mockJS Of mock-server Later, the transmission form was changed , Cause not to recognize . The solution is simple , stay axios When creating, specify header Of content-type:
const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
withCredentials: true, // send cookies when cross-domain requests
timeout: 5000, // request timeout
headers: {
'Content-Type': 'application/json; charset=UTF-8;'
}
})then express There is no need to receive it header, The default is recognition content-type Of , Of course, the configuration is also ok , You can use the following template , Add middleware :
server.all("*", function (req, res, next) {
// Set domain names that allow cross domain ,* Allows any domain name to cross domain
res.header("Access-Control-Allow-Origin", "*")
// Allow the header type
res.header("Access-Control-Allow-Headers", "*")
// How cross domain requests are allowed
res.header("Access-Control-Allow-Methods", "*")
res.header("Access-Control-Allow-Credentials", "true")
if (req.method.toLowerCase() == 'options') {
res.send(200) // Give Way options Try to request a quick end
} else {
next()
}More violent , Let it all go . Of course , You can change... As needed .
边栏推荐
- How to quickly wrap lines in Excel table
- CSP2021 T3 回文
- 交换机链路聚合详解【华为eNSP】
- Detailed analysis of common command modules of ansible service
- The R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graphs, uses the by parameter to specify the groupi
- 在EXCEL表格中如何进行快速换行
- 栈与队列——225. 用队列实现栈
- JS get object attribute value
- 字符串——剑指 Offer 58 - II. 左旋转字符串
- Hcip day 13
猜你喜欢
随机推荐
栈与队列——20. 有效的括号
Remove the treasure box app with the green logo that cannot be deleted from iPhone
Wechat applet todo case
CSDN垃圾的没有底线!
JS execution mechanism
2021年最新最全Flink系列教程_Flink原理初探和流批一体API(二.五)v2
R语言tidyr包的gather函数将从宽表转化为长表(宽表转化为长表)、第一个参数指定原多个数据列名称生成的新数据列名称、第二个参数指定原表内容值、第三个和第四个参数通过列索引指定不变的列名称列表
R语言使用epiDisplay包的tableStack函数制作统计汇总表格(基于目标变量分组的描述性统计、假设检验等)、设置by参数为目标变量、设置percent参数配置是否显示百分比信息
CSP2021 T1 廊桥分配
Apache2 ha experiment with raspberry pie
Simple order management system small exercise
Difference between code signing certificate and SSL certificate
SQL server startup and shutdown job script
Flink综合案例(九)
软链接、硬链接
Data Lake series articles
Error reported when using activiti to create a database table
Solve the problem that the ARR containsobject method returns no every time
How to install PHP 5.6 on Ubuntu 18.04 and Debian 9
Browser failed to get cookies, browser solution








