Unable to deploy my node app on A2Hosting, getting below error:
Error: ENOENT: no such file or directory, stat ‘/home/eduonli1/portfolio_app/public/index.html’
URL: psrajput.com
Node deployment page:
Run NPM Install is successful, but Run JS script keeps loading
Anyways, when I go the url, I get:
Error: ENOENT: no such file or directory, stat ‘/home/eduonli1/portfolio_app/public/index.html’
But the file exist in the above path:
My overall folder structure:
portfolio/
│
├── app.js
├── package.json
├── public/
│ ├── css/
│ │ ├── styles.css
│ ├── js/
│ │ ├── script.js
│ ├── images/
│ │ ├── image1.jpg
│ │ └── image2.png
│ ├── fonts/
│ │ ├── font1.woff
│ │ └── font2.ttf
│ └── html/
│ ├── index.html
│ └── mail-success.html
└── .htaccess
app.js:
const express = require("express");
const bodyParser = require("body-parser");
const nodemailer = require("nodemailer");
const dotenv = require("dotenv");
const path = require("path");
const rateLimit = require("express-rate-limit");
dotenv.config();
const app = express();
const port = process.env.PORT || 3131;
// Middleware to parse JSON bodies
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Serve static files
app.use("/assets", express.static(path.join(__dirname, "public", "assets")));
app.use("/css", express.static(path.join(__dirname, "public", "css")));
app.use("/js", express.static(path.join(__dirname, "public", "js")));
// Rate limiting middleware
const limiter = rateLimit({
windowMs: 60 * 60 * 1000, // 1 hour
max: 5, // limit each IP to 5 requests per windowMs
message: "Too many requests from this IP, please try again later",
});
app.use("/send-email", limiter);
// Route to handle form submission
app.post("/send-email", (req, res) => {
const { name, email, message } = req.body;
console.log("Form data received:", { name, email, message });
// Create a Nodemailer transporter
const transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: process.env.GMAIL_USER,
pass: process.env.GMAIL_PASS,
},
});
// Email message options
const mailOptions = {
from: email,
to: process.env.RECIPIENT_EMAIL,
subject: "New contact form submission",
text: `Name: ${name}nEmail: ${email}nMessage: ${message}`,
};
console.log("Attempting to send email with options:", mailOptions);
// Send email
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error("Error sending email:", error);
res.status(500).send("Failed to send email");
} else {
console.log("Email sent successfully:", info.response);
// Send mail-success.html
res.sendFile(path.join(__dirname, "public", "mail-success.html"), () => {
// Redirect to index.html after 5 seconds
setTimeout(() => {
res.redirect("/");
}, 5000);
});
}
});
});
// Serve index.html for root path
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
// Start server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
2
Answers
There were few issues with my deployment:
Since the
index.html
file is insidehtml
folder, you have set the serving folder wronginstead of
res.sendFile(path.join(__dirname, "public", "index.html"));
, tryres.sendFile(path.join(__dirname, "public", "html", "index.html"));