Form gets sent in development but not when site is deployed
Answered
Singapura posted this in #help-forum
SingapuraOP
Hi guys, I made a website which has a form that the user fills and the client receives the form in their email. Problem is that it doesnt work in production. The website is hosted on namecheap.
This is the relevant code:
api/send/route.js:
And this is the code from the form(attached)
Can someone explain why this is and how to fix? Thank you
This is the relevant code:
api/send/route.js:
import { NextResponse, NextRequest } from "next/server";
const nodemailer = require("nodemailer");
export async function POST(request: any) {
const username = process.env.NEXT_MAIL_USERNAME;
const password = process.env.NEXT_MAIL_PASSWORD;
const myEmail = process.env.NEXT_MAIL_EMAIL;
const formData = await request.json();
const name = formData.name;
const lastName = formData.lastName;
const phoneNumber = formData.phoneNumber;
const emailAddress = formData.emailAddress;
const message = formData.message;
const transporter = nodemailer.createTransport({
host: "smtp.privateemail.com",
port: 587,
secure: false,
auth: {
user: username,
pass: password,
},
});
try {
const mail = await transporter.sendMail({
from: username,
to: myEmail,
replyTo: emailAddress,
subject: `Website activity from ${emailAddress}`,
html: `
<p>Name: ${name} </p>
<p>Last Name: ${lastName} </p>
<p>Phone Number: ${phoneNumber} </p>
<p>Email: ${emailAddress} </p>
<p>Message: ${message} </p>
`,
});
return NextResponse.json({ message: "Success: email was sent" });
} catch (error) {
console.error(error);
return NextResponse.json(
{ error: "Failed to send email" },
{ status: 500 }
);
}
}And this is the code from the form(attached)
Can someone explain why this is and how to fix? Thank you
Answered by Toyger
it's litespeed server, I thouhght you have VPS from namecheap.
you can't serve nextjs from litespeed. maybe only SSG, but not sure even about that.
and you are trying to run api which is server code which definetely not possible to run on it.
you can't serve nextjs from litespeed. maybe only SSG, but not sure even about that.
and you are trying to run api which is server code which definetely not possible to run on it.
24 Replies
SingapuraOP
bump
SingapuraOP
Some extra info:
When I run it on localhost:
This is the request header:
This is the failed one from the online website:
As you can see the successful one ends in /send/
Perhaps that is the issue? But im still not sure why that would happen
When I run it on localhost:
This is the request header:
Request URL:
http://localhost:3000/api/send/
Request Method:
POST
Status Code:
200 OKThis is the failed one from the online website:
Request URL:
https://websitename.info/api/send
Request Method:
POST
Status Code:
404 Not FoundAs you can see the successful one ends in /send/
Perhaps that is the issue? But im still not sure why that would happen
SingapuraOP
Can anyone help?
Toyger
you can use postman to check your url directly on production environment.
@Toyger you can use postman to check your url directly on production environment.
SingapuraOP
<p>The resource requested could not be found on this server!</p>
</div></div><div style="color:#f0f0f0; font-size:12px;margin:auto;padding:0px 30px 0px 30px;position:relative;clear:both;height:100px;margin-top:-101px;background-color:#474747;border-top: 1px solid rgba(0,0,0,0.15);box-shadow: 0 1px 0 rgba(255, 255, 255, 0.3) inset;">
<br>Proudly powered by LiteSpeed Web Server<p>Please be advised that LiteSpeed Technologies Inc. is not a web hosting company and, as such, has no control over content found on this site.</p></div></body></html>This is the error
@Singapura <p>The resource requested could not be found on this server!</p>
</div></div><div style="color:#f0f0f0; font-size:12px;margin:auto;padding:0px 30px 0px 30px;position:relative;clear:both;height:100px;margin-top:-101px;background-color:#474747;border-top: 1px solid rgba(0,0,0,0.15);box-shadow: 0 1px 0 rgba(255, 255, 255, 0.3) inset;">
<br>Proudly powered by LiteSpeed Web Server<p>Please be advised that LiteSpeed Technologies Inc. is not a web hosting company and, as such, has no control over content found on this site.</p></div></body></html>
This is the error
Toyger
it's litespeed server, I thouhght you have VPS from namecheap.
you can't serve nextjs from litespeed. maybe only SSG, but not sure even about that.
and you are trying to run api which is server code which definetely not possible to run on it.
you can't serve nextjs from litespeed. maybe only SSG, but not sure even about that.
and you are trying to run api which is server code which definetely not possible to run on it.
Answer
@Toyger it's litespeed server, I thouhght you have VPS from namecheap.
you can't serve nextjs from litespeed. maybe only SSG, but not sure even about that.
and you are trying to run api which is server code which definetely not possible to run on it.
SingapuraOP
Hmm, so what would you recommend I do in this casE?
@Singapura Hmm, so what would you recommend I do in this casE?
Toyger
you need to use VPS either host on vercel
@Toyger you need to use VPS either host on vercel
SingapuraOP
Does it cost anything? I made this website for a friend and he cant pay monthly for it. it is a small business website
or is there any other workaround for this ?
@Singapura Does it cost anything? I made this website for a friend and he cant pay monthly for it. it is a small business website
Toyger
if you don't have a lot of unoptimized code it should be fine on vercel, you only need to check your analytics because vercel have some limits which are not free is you will go over them. but still their free plan pretty generous.
about workaround as I said if you'll use SSG without any dynamics then it's probably possible to host on current hosting, but even from your send email code it's obvious you need some dynamic so it will not work.
about workaround as I said if you'll use SSG without any dynamics then it's probably possible to host on current hosting, but even from your send email code it's obvious you need some dynamic so it will not work.
@Toyger if you don't have a lot of unoptimized code it should be fine on vercel, you only need to check your analytics because vercel have some limits which are not free is you will go over them. but still their free plan pretty generous.
about workaround as I said if you'll use SSG without any dynamics then it's probably possible to host on current hosting, but even from your send email code it's obvious you need some dynamic so it will not work.
SingapuraOP
I was more concerned about their fair use policy. I read that if your profit from the website it is against their policy. This website is pretty much just information about the business and their prices. There is no subscription plans or anything like that on it. Just want to make sure its okay to host it on their free tier.
Commercial usage is defined as any Deployment that is used for the purpose of financial gain of anyone involved in any part of the production of the project, including a paid employee or consultant writing the code. Examples of this include, but are not limited to, the following:
Any method of requesting or processing payment from visitors of the site
Advertising the sale of a product or service
Receiving payment to create, update, or host the site
Affiliate linking is the primary purpose of the site
The inclusion of advertisements, including but not limited to online advertising platforms like Google AdSense
I guess not according to this
Any method of requesting or processing payment from visitors of the site
Advertising the sale of a product or service
Receiving payment to create, update, or host the site
Affiliate linking is the primary purpose of the site
The inclusion of advertisements, including but not limited to online advertising platforms like Google AdSense
I guess not according to this
I'm not sure what to do
@Singapura I was more concerned about their fair use policy. I read that if your profit from the website it is against their policy. This website is pretty much just information about the business and their prices. There is no subscription plans or anything like that on it. Just want to make sure its okay to host it on their free tier.
Toyger
it's better to pay for VPS then, because all other platforms like render.com heroku netlify etc.... have much stricter limits
@Toyger it's better to pay for VPS then, because all other platforms like render.com heroku netlify etc.... have much stricter limits
SingapuraOP
Okay, I will look into it
Thank you
SingapuraOP
@Toyger sorry to ping again but it just occured to me that on the same website I am also using the google places api and that is working perfectly fine on production. Shouldnt that also not work in a litespeed server like you said above?
@Singapura <@536484914221285376> sorry to ping again but it just occured to me that on the same website I am also using the google places api and that is working perfectly fine on production. Shouldnt that also not work in a litespeed server like you said above?
Toyger
depends on how it's implemented.
how you even run your site?
if it's some simple LAMP hosting then you can't even install node_modules.
how you even run your site?
if it's some simple LAMP hosting then you can't even install node_modules.
@Toyger depends on how it's implemented.
how you even run your site?
if it's some simple LAMP hosting then you can't even install node_modules.
SingapuraOP
I'm using the Stellar shared hosting on namecheap. its the cheapest plan
@Singapura I'm using the Stellar shared hosting on namecheap. its the cheapest plan
Toyger
it's default LAMP server, it's not capable to run nodejs runtime.
only way your page is working is if you have static export of pages, and it just exported this pages as static files.
so even if static works, with google places it's still static feature, then dynamic like your api routes will not work anyway because you can't run nodejs runtime.
only way your page is working is if you have static export of pages, and it just exported this pages as static files.
so even if static works, with google places it's still static feature, then dynamic like your api routes will not work anyway because you can't run nodejs runtime.
SingapuraOP
you are right, the only dynamic file is this one that isnt working
Okay I see
Thanks