简体   繁体   English

为 bluehost 服务器设置 Node.js 端口

[英]Setting Node.js Port for bluehost server

I'm trying to deploy a node.js and express website on my bluehost VPS via cpanel, but although the git repo is deployed, and the app is registered via the application manager, and npm dependencies are ensured when going to the URL the only thing you can see is 2 of the files from my public_html node_modules and views -- but the rest of the git repo doesn't show. I'm trying to deploy a node.js and express website on my bluehost VPS via cpanel, but although the git repo is deployed, and the app is registered via the application manager, and npm dependencies are ensured when going to the URL the only thing you可以看到我的 public_html node_modules 和视图中的 2 个文件——但是 git 存储库的 rest 没有显示。

I have a feeling that the right port isn't being set in my app.js as I think the cpanel.yml file is correct.我觉得我的 app.js 中没有设置正确的端口,因为我认为 cpanel.yml 文件是正确的。 I'm thinking it's still just trying to connect to 3000 perhaps?我在想它仍然只是试图连接到 3000 也许? Any help would be great.任何帮助都会很棒。

The app.js is as follows: app.js 如下:


// //jshint eversion:6

const http = require('http')
const express = require("express");
const bodyParser = require("body-parser")

const ejs = require('ejs');
const nodemailer = require('nodemailer');
const hostname = '127.0.0.1';
const path = require('path');
const port = 3000;


const app = express();


app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(express.json());



// custom middleware to log data access
const log = function (request, response, next) {
    console.log(`${new Date()}: ${request.protocol}://${request.get('host')}${request.originalUrl}`);
    console.log(request.body); // make sure JSON middleware is loaded first
    next();
}
app.use(log);
// end custom middleware


app.use(express.static('public'));
app.use(express.static('images'));



app.get("/", function (req, res) {
    res.render("index");
});

app.get("/prices", function (req, res, ) {
    res.render("prices");
});

app.get("/about", function (req, res, ) {
    res.render("about");
});
app.get("/contact", function (req, res, ) {
    res.render("contact");
});

module.exports = function () {
    this.Categories = require('tools.js');
}



// HTTP POST
app.post("/ajax/email", function (request, response) {
    // create reusable transporter object using the default SMTP transport
    const transporter = nodemailer.createTransport({
        host: "smtp.gmail.com",
        port: 465,
        secure: true,
        auth: {
            user: "Dxxx@gmail.com", // this should be YOUR GMAIL account
            pass: "xxxx" // this should be your password
        }
    });

    var textBody = `FROM: ${request.body.fname}  EMAIL: ${request.body.email} MESSAGE: ${request.body.comment}`;
    var htmlBody = `<h2>Mail From Contact Form</h2> <p>From: ${request.body.fname} ${request.body.lname} </p> <p> Email: ${request.body.email}</p> <p> Phone Number: ${request.body.phone}</p> <p> Company: ${request.body.company}</p><p>Comment: ${request.body.comment}</p>`;
    var mail = {
        from: "xxx", // sender address
        to: "XXX", // list of receivers (THIS COULD BE A DIFFERENT ADDRESS or ADDRESSES SEPARATED BY COMMAS)
        subject: "Mail From Contact Form", // Subject line
        text: textBody,
        html: htmlBody
    };

    // send mail with defined transport object
    transporter.sendMail(mail, function (err, info) {
        if (err) {
            console.log(err);
            response.json({
                message: "message not sent: an error occured; check the server's console log"
            });
        } 
    });
});


const PORT = process.env || port;


const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello World! NodeJS \n');
});

app.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});

cpanel.yml cpanel.yml


       ---
deployment:
 tasks:
 - export DEPLOYPATH=/home/deltade4/public_html
 - /bin/cp -r /home/deltade4/repositories/DeltaDesigns22 $DEPLOYPATH
 - /bin/cp -R node_modules $DEPLOYPATH
 - /bin/cp -R views $DEPLOYPATH
 - /bin/cp -R images $DEPLOYPATH
 - /bin/cp -R css $DEPLOYPATH
 - /bin/cp app.js $DEPLOYPATH
 - /bin/cp package.json $DEPLOYPATH
 - /bin/cp package-lock.json $DEPLOYPATH

you are using const port = 3000;您正在使用const port = 3000; as default.默认。

so use所以用

const PORT = process.env.PORT || port;

and use PORT instead of port so do like this并使用PORT而不是port所以这样做

app.listen(PORT, hostname, () => {
    console.log(`Server running at http://${hostname}:${PORT }/`);
});

Where is the port?港口在哪里?

const PORT = process.env || port;

This should be like this.这应该是这样的。

const PORT = process.env.PORT || port;

I have had the same problem, and I have used this ||我也遇到了同样的问题,我用过这个|| syntax, but my node/express server still doesn't work properly.语法,但我的节点/快递服务器仍然无法正常工作。 I just see a plaintext rendering of my index.js file.我只看到我的 index.js 文件的纯文本呈现。 I think the process.env.PORT environment variable is not setup, because I tried to console log process.env.PORT and it was undefined.我认为 process.env.PORT 环境变量没有设置,因为我试图控制台记录 process.env.PORT 并且它是未定义的。 When I used Heroku, I didn't have to set up the PORT environment variable, but now I am wondering if the PORT environment variable needs to be setup on Blue Host and not Heroku.当我使用Heroku时,我不必设置PORT环境变量,但现在我想知道是否需要在Blue Host上设置PORT环境变量而不是Heroku。 How would one go about setting up the PORT environment variable and what is the correct PORT to use for a default Blue Host VPS anyways? go 如何设置 PORT 环境变量以及用于默认 Blue Host VPS 的正确 PORT 是什么?

UPDATE: I fixed my problem by configuring process.env.PORT in the SSH terminal.更新:我通过在 SSH 终端中配置 process.env.PORT 解决了我的问题。 To do this you follow these steps.为此,请按照以下步骤操作。

PORT=3000
export PORT

At this point you should have access to process.env.PORT with the value 3000. Also make sure you use "process.env.PORT" instead of just "process.env"此时您应该可以访问值为 3000 的 process.env.PORT。还要确保使用“process.env.PORT”而不是“process.env”

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

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