|
/mnt/disks/kimuko-static/html/webtest/ |
| Current File : /mnt/disks/kimuko-static/html/webtest/server.js |
const express = require('express');
const nodemailer = require('nodemailer');
const app = express();
const PORT = 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true })); // フォームデータを解析するため
app.use(express.static('public')); // HTMLファイルを提供するため
let availableSlots = generateInitialSlots();
function generateInitialSlots() {
const slots = [];
const today = new Date();
today.setDate(today.getDate() + 1); // 明日の日付を取得
for (let i = 0; i < 7; i++) {
const day = new Date(today);
day.setDate(today.getDate() + i);
for (let hour = 10; hour < 19; hour++) {
slots.push({
date: formatDate(day),
time: `${hour}:00 - ${hour + 1}:00`,
isAvailable: Math.random() < 0.5 // 仮に50%の確率で予約済みにする
});
}
}
return slots;
}
function formatDate(date) {
const year = date.getFullYear();
const month = ('0' + (date.getMonth() + 1)).slice(-2);
const day = ('0' + date.getDate()).slice(-2);
return `${year}-${month}-${day}`;
}
app.get('/api/available-slots', (req, res) => {
res.json(availableSlots);
});
app.post('/reserve', (req, res) => {
const { date, time, email, phone } = req.body;
const slot = availableSlots.find(s => s.date === date && s.time === `${time}:00 - ${parseInt(time) + 1}:00`);
if (slot && slot.isAvailable) {
slot.isAvailable = false;
sendConfirmationEmail(email, date, `${time}:00 - ${parseInt(time) + 1}:00`);
res.send('予約が完了しました。確認メールを送信しました。');
} else {
res.send('予約に失敗しました。この時間帯は既に予約されています。');
}
});
const sendConfirmationEmail = (email, date, time) => {
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'your-email-password'
}
});
let mailOptions = {
from: '[email protected]',
to: email,
subject: '予約確認',
text: `予約が完了しました。日付: ${date}, 時間: ${time}`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
};
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});