My code works on my local server "port 5000" but it doesn’t on heroku here is a link for the code https://github.com/zaidalkhatib/RestaurantWebsite

const express = require(“express”);
const app = express();
const bodyParser = require(“body-Parser”);
const nodemailer = require(“nodemailer”);
var exphbs = require(‘express-handlebars’);
app.engine(‘handlebars’, exphbs());
app.set(‘view engine’, ‘handlebars’);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(“public”));

app.get("/menu", function(req, res) {
res.sendFile(__dirname + ("/menu.html"));
})

app.get("/", function(req, res) {
res.sendFile(__dirname + ("/index.html"));
})

app.get("/success", function(req, res) {
res.sendFile(__dirname + ("/success.html"));
})

app.get("/views/order", function(req, res) {
res.render(__dirname + ("/views/order.ejs"));
})

app.post("/order", function(req, res) {

const output = `
<h1>لديك طلب جديد </h1>
<ul>  
  <h3>الاسم: ${req.body.name}</h3>
  <h3>رقم الهاتف: ${req.body.phone}</h3>
  <h3>العنوان: ${req.body.address}</h3>
</ul>
<h3>الطلب</h3>
<h1>${req.body.details}</h1>

`;

var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: "Zaid.alkhatib97@gmail.com", // generated ethereal user
        pass: ""
    }
});

var mailOptions = {
    from: 'Zaid.alkhatib97@gmail.com',
    to: 'zalkhatib@mail.sfsu.edu',
    subject: 'Sending Email using Node.js',
    html: output
        // html: '<h1>Hi Smartherd</h1><p>Your Messsage</p>'        
};

transporter.sendMail(mailOptions, function(error, info) {
    if (error) {
        console.log(error);
        res.render(__dirname + ("/views/failed.ejs"), { msg: "email sent" });


    } else {
        console.log('Email sent: ' + info.response);
        res.render(__dirname + ("/views/success.ejs"), { msg: "email sent" });
    }

});

});

app.set(‘port’, (process.env.PORT || 5000));

// Start node server
app.listen(app.get(‘port’), function() {
console.log('Node server is running on port ’ + app.get(‘port’));
});

It seems the problem is most unlikely have anything to do with the port. Heroku automatically binds the web application to the PORT environment variable at runtime as far as I know, and your code shows that the web application should be able to use the Heroku’s pre-assigned PORT environment variable.

Do you mind to post your result of heroku logs -a app_name ?

when i do heroku logs -a send.js , it says couldn’t find app
when i do heroku logs it gives me this

when i do heroku logs -a send.js , it says couldn’t find app
when i do heroku logs it gives me this

It seems body-Parser module is not found. Are you sure it’s not body-parser instead of body-Parser?

2 Likes

yes it was body-Parser. just changed. let me see if it is going to work now!

it is working thanks . i really appreciate it

1 Like

now i have another issue , on my local host , the email gets send normally but on the heroku. it takes to the filled page!

app.post("/order", function(req, res) {

const output = `
<h1>لديك طلب جديد </h1>
<ul>  
  <h3>الاسم: ${req.body.name}</h3>
  <h3>رقم الهاتف: ${req.body.phone}</h3>
  <h3>العنوان: ${req.body.address}</h3>
</ul>
<h3>الطلب</h3>
<h1>${req.body.details}</h1>

`;

var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: "Zaid.alkhatib97@gmail.com", // generated ethereal user
        pass: ""
    }
});

var mailOptions = {
    from: 'Zaid.alkhatib97@gmail.com',
    to: 'zalkhatib@mail.sfsu.edu',
    subject: 'Sending Email using Node.js',
    html: output
        // html: '<h1>Hi Smartherd</h1><p>Your Messsage</p>'        
};

transporter.sendMail(mailOptions, function(error, info) {
    if (error) {
        console.log(error);
        res.render(__dirname + ("/views/failed.ejs"), { msg: "email sent" });


    } else {
        console.log('Email sent: ' + info.response);
        res.render(__dirname + ("/views/success.ejs"), { msg: "email sent" });
    }

});

});