[英]How to configure Node.js in Web hosting?
I made a website with create-react-app with a contact form that communicates with the backend (nodejs with nodemailer).我创建了一个带有 create-react-app 的网站,并带有一个与后端通信的联系表单(nodejs 和 nodemailer)。 In localhost works perfectly.
在 localhost 中完美运行。
When I uploaded the website in a web hosting (wnpower.com/web-hosting is the hosting I bought) that supports nodejs apps, I can't use the contact form because I get "net::ERR_CONNECTION_TIMED_OUT" in the path "https://mywebsite.com/api/sendmessage".当我在支持 nodejs 应用程序的 web 主机(wnpower.com/web-hosting 是我购买的主机)中上传网站时,我无法使用联系表格,因为我在路径“https”中得到“net::ERR_CONNECTION_TIMED_OUT” ://mywebsite.com/api/sendmessage”。 It seems the frontend can't find the backend router or something that I can't understand.
似乎前端找不到后端路由器或我无法理解的东西。
In the CPanel of the web hosting, in the terminal I installed Nodejs and ran a test app, works perfectly.在 web 主机的 CPanel 中,我在终端中安装了 Nodejs 并运行了一个测试应用程序,运行良好。 But when I want to use node app across the frontend, doesn't work.
但是当我想在前端使用节点应用程序时,它不起作用。
My configuration in the node app.js file:我在节点 app.js 文件中的配置:
require("./config"); // all process.env
const express = require("express");
const path = require('path');
const app = express();
const bodyParser = require("body-parser");
// ALL ROUTES
const contact_routes = require('./routes');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.listen(process.env.PORT, () => {
console.log("Server listening at port "+process.env.PORT);
});
// Configure Header HTTP
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Authorization, X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Request-Method"
);
res.header("Access-Control-Allow-Methods", "GET, POST");
res.header("Allow", "GET, POST");
next();
});
app.use(express.static(path.join(__dirname, '../public_html')));
// CONNECT ROUTES WITH API
app.use(process.env.API_URL, contact_routes);
module.exports = {
app
};
public_html directory are the static files that I built with the command npm run-script build and the __dirname is the server folder. public_html 目录是我使用命令 npm run-script build 构建的 static 文件,__dirname 是服务器文件夹。 So:
所以:
And in the config.js file there is the process.env.PORT and the port is 3050.在 config.js 文件中有 process.env.PORT,端口是 3050。
In the routes.js:在 routes.js 中:
var express = require('express');
var controller = require('./controller');
var router = express.Router();
router.post('/sendmessage', controller.sendMessage);
module.exports = router;
in the .htaccess file:在 .htaccess 文件中:
DirectoryIndex ""
RewriteEngine On
RewriteCond %{REQUEST_URI} ^.*/index.*
RewriteRule ^(.*)$ http://127.0.0.1:3050/ [P,L]
RewriteRule ^$ http://127.0.0.1:3050/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:3050/$1 [P,L]
I can't understand well, I have no experience in that.我不太明白,我没有这方面的经验。
My idea is我的想法是
If any information is missing, tell me I'll share the code.如果缺少任何信息,请告诉我,我将分享代码。 I need to resolve this as soon as possible.
我需要尽快解决这个问题。 Thank you for reading and sorry for my English.
谢谢你的阅读,对不起我的英语。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.