当前位置:网站首页>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 .
边栏推荐
- Detailed analysis of common command modules of ansible service
- Flink advanced features and new features (VIII) V2
- Hcip day 13
- How to verify the domain name after applying for SSL digital certificate?
- 栈与队列——232. 用栈实现队列
- 使用Activiti创建数据库表报错,
- Network security -- Service Vulnerability scanning and utilization
- Stack and queue - 225. Implement stack with queue
- Get (min / max value) of (object array / array)
- 网络安全——文件上传白名单绕过
猜你喜欢

【无标题】

position: -webkit-sticky; /* for Safari */ position: sticky;

Simple order management system small exercise

JS execution mechanism

Unity行人随机行走不碰撞

Network security - filtering bypass injection

Network security - file upload whitelist bypass

2021-07-09

Mmdrawercontroller first loading sidebar height problem

Rhcsa sixth note
随机推荐
Mmdrawercontroller first loading sidebar height problem
Chapter VI bus
Matlab program for natural gas flow calculation
数据湖系列文章
Data formatting widget
The KAP function of epidisplay package in R language calculates the value of kappa statistics (total consistency, expected consistency), analyzes the consistency of the results of multiple scoring obj
SQL subquery
Afnetworking data raw request mode
One problem encountered:
CSDN垃圾的没有底线!
R language uses the sum function of epidisplay package to calculate the descriptive statistical summary information of the specified variables in dataframe under different grouping variables, visualiz
R语言使用sort函数排序向量数据实战、返回实际排序后的数据(默认升序)
2022年全国职业院校技能大赛赛项比赛时间、承办校信息统计表(第二批)
Network security - file upload whitelist bypass
通配符(泛域名)SSL证书
Network security - filtering bypass injection
Packet switching and label switching in MPLS
栈与队列——232. 用栈实现队列
2021-07-09
Network security - file upload penetration test