node.js - 为 bluehost 服务器设置 Node.js 端口
问题描述
我正在尝试通过 cpanel 在我的 bluehost VPS 上部署 node.js 和 express 网站,但是尽管部署了 git repo,并且应用程序是通过应用程序管理器注册的,并且在访问唯一的 URL 时确保了 npm 依赖项您可以看到我的 public_html node_modules 和视图中的 2 个文件——但 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 }/`);
});
推荐阅读
- istio - 如何让 lua envoy 过滤器在 istio 集群上工作?
- javascript - 如何为每个工作人员负载平衡 1 个任务?
- python - Python中的随机梯度下降
- amazon-web-services - S3 和 .net Amazon SDK - 无法下载文件
- sqlite - 不包括 sqlite 在哪个系统上
- python - 在 PySpark 中提取特定行
- python - 这个字符串在 seaborn 散点图的调色板参数中是什么意思?
- c - 有没有一种简单的方法可以在 FPGA 上运行的 C 程序中与网页进行交互?
- php - WebSocket 连接失败:连接建立错误:net::ERR_CONNECTION_TIMED_OUT
- tfs - 无法在 TFS 中查看集合或项目