-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
136 lines (116 loc) · 3.91 KB
/
server.js
File metadata and controls
136 lines (116 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// TechStudio - 后端服务器
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const nodemailer = require('nodemailer');
const path = require('path');
// 初始化Express应用
const app = express();
const PORT = process.env.PORT || 3000;
// 中间件配置
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// 静态文件服务
app.use(express.static(path.join(__dirname, '/')));
// 数据库连接
// 注意:在实际部署时,应该使用环境变量存储MongoDB连接URI
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost:27017/techstudio';
// 尝试连接到MongoDB
mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log('已成功连接到MongoDB数据库');
}).catch(err => {
console.error('MongoDB连接错误:', err.message);
});
// 定义联系表单数据模型
const contactSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true },
company: { type: String },
service: { type: String },
message: { type: String, required: true },
createdAt: { type: Date, default: Date.now }
});
const Contact = mongoose.model('Contact', contactSchema);
// 邮件发送配置
// 注意:在实际部署时,应该使用环境变量存储邮件配置
const transporter = nodemailer.createTransport({
service: 'gmail', // 使用你的邮件服务提供商
auth: {
user: process.env.EMAIL_USER || 'your-email@gmail.com',
pass: process.env.EMAIL_PASS || 'your-email-password'
}
});
// API路由
// 联系表单提交API
app.post('/api/contact', async (req, res) => {
try {
const { name, email, company, service, message } = req.body;
// 验证必填字段
if (!name || !email || !message) {
return res.status(400).json({ success: false, message: '请填写所有必填字段' });
}
// 创建新的联系记录
const newContact = new Contact({
name,
email,
company,
service,
message
});
// 保存到数据库
await newContact.save();
// 发送通知邮件
const mailOptions = {
from: process.env.EMAIL_USER || 'your-email@gmail.com',
to: 'contact@techstudio.com', // 公司接收邮箱
subject: `新的联系请求: ${service || '咨询'}`,
html: `
<h2>新的项目咨询请求</h2>
<p><strong>姓名:</strong> ${name}</p>
<p><strong>邮箱:</strong> ${email}</p>
<p><strong>公司:</strong> ${company || '未提供'}</p>
<p><strong>服务类型:</strong> ${service || '未选择'}</p>
<p><strong>消息内容:</strong></p>
<p>${message}</p>
`
};
// 尝试发送邮件
try {
await transporter.sendMail(mailOptions);
console.log('通知邮件已发送');
} catch (emailErr) {
console.error('邮件发送失败:', emailErr);
// 注意:即使邮件发送失败,我们仍然返回成功,因为联系信息已保存到数据库
}
// 返回成功响应
res.status(201).json({
success: true,
message: '您的请求已成功提交,我们将尽快与您联系!',
data: newContact
});
} catch (error) {
console.error('处理联系表单时出错:', error);
res.status(500).json({
success: false,
message: '服务器处理请求时出错,请稍后再试'
});
}
});
// 服务状态检查API
app.get('/api/status', (req, res) => {
res.json({ status: 'online', message: 'TechStudio API服务正常运行' });
});
// 确保所有其他路由都返回到前端应用
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// 启动服务器
app.listen(PORT, () => {
console.log(`服务器已在端口 ${PORT} 上启动`);
});