繁体   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可以看到我的 public_html node_modules 和视图中的 2 个文件——但是 git 存储库的 rest 没有显示。

我觉得我的 app.js 中没有设置正确的端口,因为我认为 cpanel.yml 文件是正确的。 我在想它仍然只是试图连接到 3000 也许? 任何帮助都会很棒。

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


       ---
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

您正在使用const port = 3000; 默认。

所以用

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

并使用PORT而不是port所以这样做

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

港口在哪里?

const PORT = process.env || port;

这应该是这样的。

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

我也遇到了同样的问题,我用过这个|| 语法,但我的节点/快递服务器仍然无法正常工作。 我只看到我的 index.js 文件的纯文本呈现。 我认为 process.env.PORT 环境变量没有设置,因为我试图控制台记录 process.env.PORT 并且它是未定义的。 当我使用Heroku时,我不必设置PORT环境变量,但现在我想知道是否需要在Blue Host上设置PORT环境变量而不是Heroku。 go 如何设置 PORT 环境变量以及用于默认 Blue Host VPS 的正确 PORT 是什么?

更新:我通过在 SSH 终端中配置 process.env.PORT 解决了我的问题。 为此,请按照以下步骤操作。

PORT=3000
export PORT

此时您应该可以访问值为 3000 的 process.env.PORT。还要确保使用“process.env.PORT”而不是“process.env”

暂无
暂无

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

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