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 have a feeling that the right port isn’t being set in my app.js as I think the cpanel.yml file is correct. I’m thinking it’s still just trying to connect to 3000 perhaps? Any help would be great.
The app.js is as follows:
// //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: "[email protected]", // 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
3
Answers
Where is the port?
This should be like this.
you are using
const port = 3000;
as default.so use
and use
PORT
instead ofport
so do like thisI 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. I think the process.env.PORT environment variable is not setup, because I tried to console log process.env.PORT and it was undefined. 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. 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?
UPDATE:
I fixed my problem by configuring process.env.PORT in the SSH terminal. To do this you follow these steps.
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"