I'm running into an issue to deploy a nuxt-app with some serverMiddleware code on Heroku.
this is my middleware code which just launch an express app:
api/index.js
var express = require("express");
var nodemailer = require("nodemailer");
var app = express();
app.use(express.json());
app.use(
express.urlencoded({
extended: true
})
);
app.get("/", (req, res) => {
res.send("hello world");
});
app.post("/contact", async (req, res) => {
// let testAccount = await nodemailer.createTestAccount();
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "mail.exemple.net",
port: 587,
secure: false,
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
}
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: req.body.email,
to: "contact@email.com",
subject: "new contact",
text: req.body.message
});
res.end();
});
app.listen(process.env.PORT);
module.exports = {
path: "/api",
handler: app
};
then i registered this file into nuxt.config.js:
serverMiddleware: ["~/api/index"],
the api works perfectly in dev mode (locally).
I first deployed on heroku with the nuxt target setting set to static,
the static content was working perfectly but none of the serverMiddleware code was working (that make senses ^^).
then i switched to target: 'server' but now when i deploy i get this message from heroku:
ā Nuxt Fatal Error
Error: Address `0.0.0.0:41828` is already in use
i'm sure that i'm missing something but can't find the solution.
just to complete my post i added this line into the package.json file:
"heroku-postbuild": "nuxt build"
and also a Procfile at the root of my app which contain:
web: nuxt start
and finally these are my heroku:config settings:
heroku config:set NPM_CONFIG_PRODUCTION=false
heroku config:set HOST=0.0.0.0
heroku config:set NODE_ENV=production
Any advice on the correct way to deploy much appreciated.