Я довольно новичок в node.js. Я пытаюсь использовать nodemailer для отправки электронного письма, и в настоящее время я получаю сообщение об ошибке типа на консоли.
// ошибка //
[0] TypeError: express.createServer не является функцией
[0] в app.post (/Users/thomashunt/Desktop/react_express_starter/server.js:33:17)
[0] в Layer.handle [as handle_request] (/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/layer.js:95:5)
[0] в следующем (/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/route.js:137:13)
[0] в Route.dispatch (/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/route.js:112:3)
[0] в Layer.handle [as handle_request] (/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/layer.js:95:5)
[0] в /Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/index.js:281:22
[0] в Function.process_params (/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/index.js:335:12)
[0] в следующем (/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/index.js:275:10)
[0] at urlencodedParser (/Users/thomashunt/Desktop/react_express_starter/node_modules/body-parser/lib/types/urlencoded.js:82:7)
[0] в Layer.handle [as handle_request] (/Users/thomashunt/Desktop/react_express_starter/node_modules/express/lib/router/layer.js:95:5)
// конец ошибки
вот код моего файла server.js:
const nodemailer = require('nodemailer')
const path = require('path')
const express = require('express')
const app = express()
const port = 5000
const cors = require('cors')
app.use(cors())
const bodyParser = require('body-parser')
app.use(bodyParser.json())
// to support JSON-encoded bodies
app.use(
bodyParser.urlencoded({
// to support URL-encoded bodies
extended: true
})
)
app.get('/home', (req, res) => {
console.log(
'Hello from .get /home',
req.body.generalDetails,
req.body.firstName,
req.body.mName
)
})
app.post('/home', (req, res) => {
var express = require('express'),
nodemailer = require("nodemailer");
app = express.createServer();
app.use(express.bodyParser());
app.post('/formProcess', function (req, res) {
var data=req.body;
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "thomas.hunt@careertrackers.org.au",
pass: "pass"
}});
smtpTransport.sendMail({ //email options
from: "email",
to: "thomas.hunt@careertrackers.org.au", // receiver
subject: "Emailing with nodemailer", // subject
text:req.body.generalDetails,
html: "here your data goes"// body (var data which we've declared)
}, function(error, response){ //callback
if(error){
console.log(error);
}else{
console.log("Message sent: " + res.message);
}
smtpTransport.close();
}); });
let data = [{
//page one data
generalDetails: req.body.generalDetails,
fName: req.body.fName,
mName: req.body.mName,
lName: req.body.lName,
email: req.body.email,
}];
res.json(data);
}
);
app.listen(port, () => `Server running on port ${port}`);