点击登录的时候option请求是通过的,但post请求失败。
服务端代码app.js:
`
const express = require(‘express’)
const bodyParser = require(‘body-parser’)
const cors = require(‘cors’)
const router = require(’./router/index’)
// 创建 express 应用
const app = express()
app.use(cors())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
// 监听 / 路径的 get 请求
app.get(’/’,router)
// 使 express 监听18082
const server = app.listen(18082, function() {
console.log(‘Http Server is running on 18082’)
})`
user.js代码
const express = require(‘express’)
const router = express.Router()
// 创建请求接口
router.post(’/login’, function (req,res) {
console.log(’/login’,req.body);
res.json({
code: 0,
mes:‘登陆成功’
})
})
module.exports = router
index.js:
const express = require(‘express’)
const boom = require(‘boom’)
const userRouter = require(’./user’)
const {
CODE_ERROR
} = require(’…/utils/constant.js’)
// 注册路由
const router = express.Router()
router.get(’/’, function(req, res) {
res.send(‘欢迎管理后台接口’)
})
router.use(’/user’, userRouter)
router.use((req, res, next) => {
next(boom.notFound(‘接口不存在’))
})
router.use((err, req, res, next) => {
const msg = (err && err.message) || '系统错误’
const statusCode = (err.output && err.output.statusCode) || 500;
const errorMsg = (err.output && err.output.payload && err.output.payload.error) || err.message
res.status(statusCode).json({
code: CODE_ERROR,
msg,
error: statusCode,
errorMsg
})
})
module.exports = router