简体   繁体   English

如何在 Web 主机中配置 Node.js?

[英]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:所以:

  • Directory:目录:
    • public_html -> static files frontend. public_html -> static 文件前端。
    • server -> node app, routes, controllers, etc.服务器 -> 节点应用程序、路由、控制器等。

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我的想法是

  1. Run the node app with nohup in the terminal of cpanel so the Node app will always running.在 cpanel 的终端中使用 nohup 运行节点应用程序,以便节点应用程序将始终运行。 Checked!检查!
  2. Try the contact form in the frontend website.尝试前端网站中的联系表。 Fail.失败。 I get timeout connection and I get nothing.我得到超时连接,但我什么也没得到。
  3. In the node app.js I want to see the console.log() in the terminal cpanel when I use the contact form.在节点 app.js 中,当我使用联系表单时,我想在终端 cpanel 中看到 console.log()。 It's for test and know thats all is ok, but I can't see anything in the terminal.这是为了测试并且知道这一切都很好,但我在终端中看不到任何东西。 How can I do that?我怎样才能做到这一点?

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.谢谢你的阅读,对不起我的英语。

  1. Try using https://mywebsite.com:3050/ instead of http://127.0.0.1:3050/ at all places in your.htaaccess file尝试使用https://mywebsite.com:3050/而不是http://127.0.0.1:3050/在您的所有地方
  2. You can get logs of nohup from tail command in its log file.您可以在其日志文件中从 tail 命令获取 nohup 的日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM