Я пытаюсь отправить простой POST
запрос на контактную форму на статическом сайте, построенном поверх NuxtJs.
Я пытаюсь использовать express
и nodemailer
в serverMiddleware
Nuxt обеспечивает
вот код от api/contact.js
const express = require('express')
const nodemailer = require('nodemailer')
const validator = require('validator')
const xssFilters = require('xss-filters')
const app = express()
app.use(express.json())
const rejectFunctions = new Map([
['name', a => a.trim().length < 4],
['email', v => !validator.isEmail(v)],
['message', v => v.trim().length < 10]
])
const validateAndSanitize = (key, value) => {
return (
rejectFunctions.has(key) &&
!rejectFunctions.get(key)(value) &&
xssFilters.inHTMLData(value)
)
}
const sendMail = (name, email, msg) => {
const transporter = nodemailer.createTransport({
sendmail: true,
newline: 'unix',
path: '/usr/sbin/sendmail'
})
transporter.sendMail(
{
from: email,
to: 'johndoe@mail.com',
subject: 'New Contact Form',
text: msg
}
)
}
app.post('/', (req, res) => {
const attributes = ['name', 'email', 'message']
const sanitizedAttributes = attributes.map(attr =>
validateAndSanitize(attr, req.body[attr])
)
const someInvalid = sanitizedAttributes.some(r => !r)
if (someInvalid) {
return res
.status(422)
.json({ error: 'Error : at least one fiel is invalid' })
}
sendMail(...sanitizedAttributes)
res.status(200).json({ message: 'OH YEAH' })
})
export default {
path: '/api/contact',
handler: app
}
Все работает как положено до функции sendMail
, где я получаю эту ошибку:
(node:10266) UnhandledPromiseRejectionWarning: Error: spawn /usr/sbin/sendmail ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:246:19)
at onErrorNT (internal/child_process.js:427:16)
at processTicksAndRejections (internal/process/next_tick.js:76:17)
(node:10266) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 6)
Есть идеи, что я делаю не так?