Uncaught SyntaxError: Unexpected token < for GET requests in production mode
Uncaught SyntaxError: Unexpected token < for GET requests in production mode
我已经在 Heroku 中部署了我的程序(Node 后端和 React 前端通过 create-react-app)。它适用于 POST 请求,但对于我收到的 GET 请求
Uncaught SyntaxError: Unexpected token <
我在 app.js 中有此代码用于前端文件夹中构建文件夹的绝对路径:
let root = path.join(__dirname, 'front-end', 'build'); // (on Heroku ==>
path.join(__dirname, 'front-end', 'build'); )
app.use(express.static(root));
app.use(function(req, res, next) {
if (req.method === 'GET' && req.accepts('html') && !req.is('json') &&!req.path.includes('.')) {
res.sendFile('index.html', { root });
} else next();
});
如何修复此部分以使其也适用于 GET 请求?
在此先感谢您的帮助。
我找到了更改的答案,也将 header 添加到我在 front-end 中的获取请求中因为我正在检查
req.method === 'GET'
所以我在请求中添加了 header:
fetch('/alladmins', {
headers: {"Content-Type": "application/json",
"Accept" : "application/json"}
})
.then(data => data.json())
.then((data) => { this.setState({ arrayOfAdmins: data });
}
,
function (error) {
console.error("Error with fetching /alladmins url:", error);
});
它现在可以工作了,有点奇怪,因为 GET 不需要这个 header 但它正在工作。
我已经在 Heroku 中部署了我的程序(Node 后端和 React 前端通过 create-react-app)。它适用于 POST 请求,但对于我收到的 GET 请求
Uncaught SyntaxError: Unexpected token <
我在 app.js 中有此代码用于前端文件夹中构建文件夹的绝对路径:
let root = path.join(__dirname, 'front-end', 'build'); // (on Heroku ==>
path.join(__dirname, 'front-end', 'build'); )
app.use(express.static(root));
app.use(function(req, res, next) {
if (req.method === 'GET' && req.accepts('html') && !req.is('json') &&!req.path.includes('.')) {
res.sendFile('index.html', { root });
} else next();
});
如何修复此部分以使其也适用于 GET 请求?
在此先感谢您的帮助。
我找到了更改的答案,也将 header 添加到我在 front-end 中的获取请求中因为我正在检查
req.method === 'GET'
所以我在请求中添加了 header:
fetch('/alladmins', {
headers: {"Content-Type": "application/json",
"Accept" : "application/json"}
})
.then(data => data.json())
.then((data) => { this.setState({ arrayOfAdmins: data });
}
,
function (error) {
console.error("Error with fetching /alladmins url:", error);
});
它现在可以工作了,有点奇怪,因为 GET 不需要这个 header 但它正在工作。