Nextjs front End and Express for smtp
Unanswered
Bee posted this in #help-forum
BeeOP
I have next js website. I deployed it on vercel. Now there is a folder named server in the directory which has express server in it. The express server is not any backend but it is just used for smtp purpose. How can i just use the mailing function built in express js for my nextjs website
7 Replies
BeeOP
I have this code in backend
const multer = require("multer");
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "./public/temp");
},
filename: function (req, file, cb) {
const originalname = file.originalname;
const filenameWithoutExtension = path.parse(originalname).name; // Extract filename without extension
const extension = originalname.split(".").pop();
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
cb(null, filenameWithoutExtension + "-" + uniqueSuffix + "." + extension);
},
});
const upload = multer({ storage: storage }).array("files", 5); // Allow up to 5 files
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());
app.get("/api/upload", (req, res) => {
res.send("Hello World!");
});
app.post("/api/upload", (req, res) => {
....
});
module.exports = app;🔴 🔴 🔴 🔴 🔴 🔴 🔴 🔴 🔴 🔴 🔴 🔴 🔴
my front end code is
my front end code is
const handleUpload = async () => {
const formData = new FormData();
// Append each file to the FormData object
files.forEach((file) => {
formData.append("files", file);
});
formData.append("email", email);
formData.append("phoneNumber", phoneNumber);
try {
const response = await fetch("https://essay-help-xug9.vercel.app/api/upload", {
method: "POST",
body: formData,
});
console.log("server starts working at client");
if (response.ok) {
const data = await response.json();
// Show a success toast
toast.success(data.message);
} else {
const data = await response.json();
// Show an error toast
toast.error(data.message);
}
} catch (error) {
console.error("Error uploading files:", error);
}
};Now i don't know how to make the backend work just to send mails from the front end
Can share more details If required
This is my file structure
Note that my backend is located in app/api/sendemail to make vercel recognise it as a serverless function
Note that my backend is located in app/api/sendemail to make vercel recognise it as a serverless function
Another sussy thing is that vercel also recognises something from nodemodule as function
here