[英]React App deployed on Digital ocean gives 404 Error
I have a validation form on React but on clicking submit m getting "404 Error".我有一个关于 React 的验证表单,但是在单击提交时出现“404 错误”。 It works perfect on local machine.
它在本地机器上完美运行。 //from app.js
//来自app.js
handleSubmit = async e => {
e.preventDefault();
const response = await fetch('/api/emailVerifier', {
method: 'POST',
body: this.state,
headers: {
'Content-Type': 'application/json',
},
//body: JSON.stringify({ post: this.state.post }),
});
const body = await response.text();
this.setState({ responseToPost: body });
};
//from server.js //来自 server.js
app.post('/api/emailVerifier', async (req, res) => {
console.log(req.body);
const emailValidator = new EmailValidator();
const { wellFormed, validDomain, validMailbox } = await emailValidator.verify(req.body.post);
res.send(
`response received , welllformed = ${wellFormed}, validDomain = ${validDomain}, validMailbox = ${validMailbox}`,
);
});
i have installed my my validator on server using npm i add deep-email-validator --save我已经使用 npm 在服务器上安装了我的验证器我添加了 deep-email-validator --save
My Directory on server has我的服务器上的目录有
asset-manifest.json index.html manifest.json package.json static
build logo192.png node_modules robots.txt
favicon.ico logo512.png package-lock.json server.js
Can you access other endpoints in server.js on DigitalOcean?您可以访问 DigitalOcean 上 server.js 中的其他端点吗? EG: if you create:
例如:如果您创建:
app.get('/', function (req, res) {
res.send('hello world')
})
And make a request to it from your React app does it reach that endpoint?并从您的 React 应用程序向它发出请求,它会到达那个端点吗?
I think you will struggle to run the React app and server from the same location.我认为您将很难从同一位置运行 React 应用程序和服务器。
If your React app is setup correctly it will try to handle all requests coming to that url.如果您的 React 应用程序设置正确,它将尝试处理来自该 url 的所有请求。
You could run the express server on a different port to the React app.你可以在与 React 应用程序不同的端口上运行 express 服务器。 Then traffic won't get routed to both the React server and your Express server.
然后流量不会同时路由到 React 服务器和您的 Express 服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.