简体   繁体   中英

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). In localhost works perfectly.

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". 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. But when I want to use node app across the frontend, doesn't work.

My configuration in the node app.js file:

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. So:

  • Directory:
    • public_html -> static files frontend.
    • server -> node app, routes, controllers, etc.

And in the config.js file there is the process.env.PORT and the port is 3050.

In the 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:

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. 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. 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
  2. You can get logs of nohup from tail command in its log file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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