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



我试图通过cpanel在我的bluehost VPS上部署node.js和express网站,但是尽管部署了git repo,并且通过应用程序管理器注册了应用程序,并且确保了npm依赖当转到URL时,你唯一能看到的是我的public_html node_modules和views中的两个文件——但是git repo的其余部分没有显示。

我有一种感觉,正确的端口没有被设置在我的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环境变量,但是现在我想知道PORT环境变量是否需要在Blue Host而不是Heroku上设置。如何设置PORT环境变量?对于默认的Blue Host VPS,正确的PORT是什么?

更新:我通过在SSH终端配置process.env.PORT解决了这个问题。要做到这一点,请遵循以下步骤。

PORT=3000
export PORT
此时,您应该可以访问process.env.PORT,其值为3000。还要确保使用"process.env. port";而不仅仅是"process.env"

最新更新