我对node.js还是很陌生,我正在尝试使用nodemailer发送电子邮件,并且目前在控制台上遇到类型错误。
//错误// [0] TypeError:express.createServer不是函数 app.post上的[0](/Users/thomashunt/Desktop/react_express_starter/server.js:33:17) [0]在Layer.handle [作为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 [作为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]在urlencodedParser(/Users/thomashunt/Desktop/react_express_starter/node_modules/body-parser/lib/types/urlencoded.js:82:7) [0]在Layer.handle [作为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}`);
答案 0 :(得分:0)
我认为您对Discount d1 = new Discount("CODE_1");
Discount d2 = new Discount("CODE_2");
List<Discount> discounts = List.of(d1, d2);
discounts.forEach(discount ->
discount.setDesc(TypeCode.valueOf(discount.getTypeCode()).getDesc()));
模块感到困惑。替换您的
http
与
app = express.createServer();
如果您想使用app = express();
,则应执行以下操作
createServer