我试图将图像实时上传到“ upload_product”目录中 子域服务器,其中一个放置在数字海洋飞沫上。我的 使用angular6,nodeJ,expressJs构建应用程序,并通过以下方式进行部署 在ubuntu上使用nginix Web服务器进行反向代理。客户 (代理使用域),服务器(代理使用子域)和数据库(mongodb)是 在同一滴中
现在,表单可以正常工作而没有图像。但是,如果我尝试使用图像提交表单,那么我会收到CORS政策:否'Access-Control-Allow-Origin'。 我已经使用了app.use(cors());在app.js中。 但我仍然遇到这个问题。在本地计算机上运行良好
const jwt = require('jsonwebtoken');
require('./config/config');
require('./models/db');
const express = require('express');
const bodyParser = require('body-parser');
const ip = require('ip');
const config = require('./config/config_pg.json');
const path = require('path');
const cors = require('cors');
const authIndex = require('./routes/index.router');
const servicesIndex = require('./routes/service.router');
var app = express();
app.locals.ipAdr = ip.address();
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(cors());
// route middleware to verify a token
app.use(function(req, res, next) {
// check header or url parameters or post parameters for token
var token = req.headers['x-access-token'] || req.headers['authorization'];
// decode token
if (token) {
if (token.startsWith('Bearer ')) {
// Remove Bearer from string
token = token.slice(7, token.length);
// convert string to token format
token = (token.split('"')[1]).trim();
}
if(!token) {
// process non-token request here, because appropriate api have token mandatary flow
next();
}
const secretKey = process.env.SecretKey;
// verifies secret and checks exp
try{
jwt.verify(token, secretKey, function(err, decoded) {
if (err) {
return res.status(401).json({ failed: 'Unauthorized Access' });
} else {
// if everything is good, save to request for use in other routes
req.decoded = decoded;
next();
}
});
} catch(error) {
next();
}
} else {
// process non-token request here, because appropriate api have token mandatary flow
next();
}
// else {
// // if there is no token
// // return an error
// return res.status(403).send({
// success: false,
// message: 'No token provided.'
// });
// }
});
// middleware
app.use('/auth', authIndex);
app.use('/api', servicesIndex);
// share static folders
app.use('/upload_product', express.static(path.join(__dirname, '/upload_product')));
// error handler
app.use((err, req, res, next) => {
if (err.name === 'ValidationError') {
var valErrors = [];
Object.keys(err.errors).forEach(key => valErrors.push(err.errors[key].message));
//res.status(422).send(valErrors)
}
});
// start server
app.listen(process.env.PORT, () => console.log(`Server started at port : ${process.env.PORT}`));
预期结果: 图片必须与表单数据一起上传
实际结果:
我得到了答复”
502错误的网关
”以及响应标头不包含cors支持标头。
Access to XMLHttpRequest at 'https://services.suriyashopping.com/api/registerBrand' from origin 'https://suriyashopping.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
后端返回的代码为0,正文为:[object ProgressEvent]