我是Node.js的新手,我正在尝试使用NodeMailer发送电子邮件,并且我当前在控制台上遇到类型错误。
//错误//[0] TypeError:Express.Createserver不是功能[0]在app.post(/Users/thomashunt/desktop/react_express_starter/server.js:33:17([0]在layer.handle [as hander_request](/users/thomashunt/desktop/react_express_starter/node_modules/express/lib/lib/router/layer.js:95:5:5([0]在下一步(/users/thomashunt/desktop/react_express_starter/node_modules/express/lib/router/route.js:137:13([0]在rout.dispatch(/Users/thomashunt/desktop/react_express_starter/node_modules/express/lib/router/router/route.js:112:3([0]在layer.handle [as hander_request](/users/thomashunt/desktop/react_express_starter/node_modules/express/lib/lib/router/layer.js:95:5:5([0] at/users/thomashunt/desktop/react_express_starter/node_modules/express/lib/router/router/index.js:281:22[0]在function.process_params(/Users/thomashunt/desktop/react_express_starter/node_modules/express/lib/lib/router/router/index.js:335:12(([0]在下一步(/users/thomashunt/desktop/react_express_starter/node_modules/express/lib/router/router/index.js:275:10([0]在urlencodedparser(/users/thomashunt/desktop/react_express_starter/node_modules/body-parser/lib-parser/lib/types/urlencoded.js:82:82:7([0]在layer.handle [as hander_request](/users/thomashunt/desktop/react_express_starter/node_modules/express/lib/lib/router/layer.js:95:5:5:5:5: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}`);
我认为您与http
模块相混淆。更换您的
app = express.createServer();
app = express();
如果要使用createServer
,则应执行以下
const http = require('http');
const express = require('express');
var app = express();
... your route config
var server = http.createServer(app)
server.listen(3000, function () {
console.log('Express is running on port 3000')
})