Javascript 联系人表单可以在本地主机上使用,但不能在html页面上使用

Javascript 联系人表单可以在本地主机上使用,但不能在html页面上使用,javascript,node.js,express,localhost,Javascript,Node.js,Express,Localhost,我正在使用node和express通过html页面发送电子邮件。 以下是我的应用程序: `const express = require('express'); const path = require('path'); const nodeMailer = require('nodemailer'); const bodyPaser = require('body-parser'); const app = express() app.get('/', function (req

我正在使用
node
express
通过
html
页面发送电子邮件。 以下是我的应用程序:

`const express = require('express');
 const path = require('path');
 const nodeMailer = require('nodemailer');
 const bodyPaser = require('body-parser');

 const app = express()

 app.get('/', function (req, res) {
 res.sendFile(path.join(__dirname + '/index.html'));
 });


 app.set('view engine', 'html');
app.use(express.static('public'));
app.use(bodyPaser.urlencoded({ extended: true }));
app.use(bodyPaser.json());
var port = 3000;
app.get('/', function (req, res) {
res.render('index.html');
app.locals.layout = false;
});


 //internet connectivity check out
function checkInternet(cb) { 
require('dns').lookup('google.com', function (err) { 
    if (err && err.code == "ENOTFOUND") {
        cb(false);
    } else { 
        cb(true);
    }
})
}


app.post('/', (req, res) => {
const output = `
<p>You have a new contact request</p>
<h3>Contact Details</h3>
<ul>
<li>Name:${req.body.name}</li>
<li>Email:${req.body.email}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`;

// create reusable transporter object using the default SMTP transport
let transporter = nodeMailer.createTransport({
    host: "mail.example.com",
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
        user: 'contact@example.com', // generated ethereal user
        pass: 'Y@$972200424' // generated ethereal password
    },
    tls: {
        rejectUnauthorized: false
    }
});

// send mail with defined transport object
let mailOptions = {
    from: `${req.body.email}`, // sender address
    to: "contact@example.com", // list of receivers
    subject: "Customer Message", // Subject line
    text: `SenderName: ${req.body.name}, --Message: ${req.body.message}`, // plain text body
    html: output // html body
};

transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log(error);
    }

    console.log("Message sent: %s", info.messageId);
    console.log("Preview URL: %s", nodeMailer.getTestMessageUrl(info));

    if (info.messageId) {
        res.redirect('back');
     res.send(true);
    }
});
});


app.listen(3000, () => console.log('server is running at port 3000'));`
`const express=require('express');
const path=require('path');
const nodeMailer=require('nodeMailer');
const bodyPaser=require('body-parser');
const app=express()
app.get('/',函数(req,res){
res.sendFile(path.join(uu dirname+'/index.html');
});
app.set('view engine','html');
应用程序使用(express.static('public'));
use(bodyPaser.urlencoded({extended:true}));
use(bodyPaser.json());
var端口=3000;
app.get('/',函数(req,res){
res.render('index.html');
app.locals.layout=false;
});
//互联网连接检查
功能检查互联网(cb){
需要('dns')。查找('google.com',函数(err){
if(err&&err.code==“ENOTFOUND”){
cb(假);
}否则{
cb(真);
}
})
}
应用程序发布(“/”,(请求,回复)=>{
常量输出=`
您有一个新的联系请求

联系方式
  • 名称:${req.body.Name}
  • 电子邮件:${req.body.Email}
消息 ${req.body.message}

`; //使用默认SMTP传输创建可重用的传输对象 让transporter=nodeEmailer.createTransport({ 主持人:“mail.example.com”, 港口:465, 安全:true,//对于465为true,对于其他端口为false 认证:{ 用户:'contact@example.com“,//生成的ethereal用户 pass:'Y@$972200424'//生成的以太密码 }, tls:{ 拒绝:错误 } }); //使用定义的传输对象发送邮件 让邮件选项={ 发件人:`${req.body.email}`,//发件人地址 至:contact@example.com“,//接收者列表 主题:“客户消息”,//主题行 text:`SenderName:${req.body.name},--Message:${req.body.Message}`,//纯文本正文 html:output//html正文 }; transporter.sendMail(邮件选项,(错误,信息)=>{ 如果(错误){ 返回console.log(错误); } console.log(“发送的消息:%s”,info.messageId); log(“预览URL:%s”,nodeEmailer.getTestMessageUrl(info)); if(info.messageId){ res.redirect(“back”); res.send(真); } }); }); app.listen(3000,()=>console.log('服务器在端口3000上运行')`
问题是它发送电子邮件,在
localhost:3000
上运行良好,但在
index.html
页面上不起作用。
如何让它在html页面上发送电子邮件?

只需将html文件移动到下面的公共目录即可 你必须改变

app.use(express.static('public'));

也改变了:

 app.get('/', function (req, res) {
 res.sendFile(path.join(__dirname + '/index.html'));
 });

按照行的顺序:

app.use(express.static(`${__dirname}/public`));
app.get('/', function (req, res) {
  res.sendFile(path.join(`${__dirname}/public/index.html`));
});
也可以使用模板引擎pug。见本文件:


只需将html文件移动到下面的公共目录即可 你必须改变

app.use(express.static('public'));

也改变了:

 app.get('/', function (req, res) {
 res.sendFile(path.join(__dirname + '/index.html'));
 });

按照行的顺序:

app.use(express.static(`${__dirname}/public`));
app.get('/', function (req, res) {
  res.sendFile(path.join(`${__dirname}/public/index.html`));
});
也可以使用模板引擎pug。见本文件:


但你知道,它以前是有效的,但几周后就不起作用了!我还没有对您的端口做任何更改,请将您的端口
端口:465
更改为另一个号码,然后再次检查。有时它恰好来自端口,如什么端口?请检查此
端口:587
另一个端口:
25或465或587或2525
(smtp)和(pop3)
1100或9950
端口。但您知道,它之前工作正常,但几周后开始不工作!我还没有对您的端口做任何更改,请将您的端口
端口:465
更改为另一个号码,然后再次检查。有时它恰好来自端口,如什么端口?请检查此
端口:587
其他端口:
25或465或587或2525
(smtp)和(pop3)
1100或9950
端口