首頁  >  文章  >  web前端  >  利用Node.js實現線上預約功能的Web項目

利用Node.js實現線上預約功能的Web項目

王林
王林原創
2023-11-08 16:33:111484瀏覽

利用Node.js實現線上預約功能的Web項目

隨著網路的不斷發展,越來越多的商家開始使用線上預約系統來方便客戶預約和管理業務。在這種情況下,利用Node.js實現線上預約功能的Web專案正逐漸成為一個熱門的話題。

在這篇文章中,我們將簡單介紹如何使用Node.js開發一個基於Web的線上預約系統,並提供一些程式碼範例,在您開始製作自己的線上預約系統之前,其希望能夠幫助您了解這個過程。

  1. 需求分析

在開發這樣一個Web專案之前,我們需要先進行仔細的需求分析。以下是一些必要的功能和特性,在這個Web專案中需要實現的:

  • 客戶能夠在網站上查看和選擇可用時間和服務項目。
  • 客戶可以選擇任何一個合適的時間,並向商家發送請求。
  • 商家可以查看預約要求,並接受或拒絕它們。
  • 當商家接受預約要求時,系統會把預約資訊輸入資料庫並發送確認電子郵件給客戶。
  • 當商家拒絕預約要求時,系統會向客戶發送拒絕電子郵件。
  1. 資料庫設計

在本例中,我們將使用MySQL資料庫來保存預約資訊。需要建立一個名為「Appointments」的表,它將包含以下列:

  • ID(唯一識別碼)
  • #客戶名稱
  • #客戶電子郵件
  • 服務類型
  • 日期
  • 時間
  • 狀態(待處理、接受或拒絕)

下面是可以用來建立該表的SQL語句:

CREATE TABLE Appointments (
  ID INT NOT NULL AUTO_INCREMENT,
  CustomerName VARCHAR(50),
  CustomerEmail VARCHAR(50),
  ServiceType VARCHAR(50),
  AppointmentDate DATE,
  AppointmentTime TIME,
  Status ENUM('Pending', 'Accepted', 'Rejected'),
  PRIMARY KEY (ID)
);
  1. 建立Node.js應用程式

首先,我們需要在本機上安裝Node.js。現在,我們將建立一個名為「AppointmentSystem」的Node.js應用程式。

建立一個名為「AppointmentSystem」的資料夾,並在資料夾中建立一個名為「app.js」的檔案。這個文件將是我們的Node.js應用程式的主文件,並包含我們的所有程式碼。

依照下列步驟安裝所需的第三方Node.js模組:

  1. 開啟命令提示字元或終端,並切換到「AppointmentSystem」資料夾。
  2. 執行下列指令:npm init
  3. 使用預設值回答所有提示問題,完成「package.json」檔案的建立。
  4. 安裝以下模組:
npm install express ejs nodemailer mysql body-parser express-session --save
  • express:這是一個流行的Node.js框架,用於建立Web應用程式。
  • ejs:這是一個範本引擎,可以動態產生HTML頁面。
  • nodemailer:這是一個Node.js模組,用於向客戶發送電子郵件。
  • mysql:這是一個Node.js模組,用來連接MySQL資料庫。
  • body-parser:這是一個Node.js中間件,用來解析HTTP請求的主體。
  • express-session:這是一個Node.js中間件,用來處理會話。
  1. 程式碼實作

首先,我們需要在我們的主檔案「app.js」中引入我們剛剛安裝的所有模組:

const express = require('express');
const ejs = require('ejs');
const nodemailer = require('nodemailer');
const mysql = require('mysql');
const bodyParser = require('body-parser');
const session = require('express-session');

const app = express();

接下來,我們需要設定應用程式。我們的應用程式將使用預設的“views”和“public”資料夾,因此我們無需對其進行配置。

app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(session({
  secret: 'mysecretkey',
  resave: true,
  saveUninitialized: true
}));

接著,我們需要連接到MySQL資料庫。我們將使用「createConnection」函數來建立一個資料庫連接,然後使用該物件進行查詢。

const pool = mysql.createPool({
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'appointments'
});

現在,我們接下來定義我們的路由功能,我們定義兩個路線,一個用於取得預約表格,另一個用於提交運單。

app.get('/', (req, res) => {
  res.render('index');
});

app.post('/appointment', (req, res) => {
  const {customerName, customerEmail, serviceType, appointmentDate, appointmentTime} = req.body;

  pool.query('INSERT INTO Appointments SET ?', {
    CustomerName: customerName,
    CustomerEmail: customerEmail,
    ServiceType: serviceType,
    AppointmentDate: appointmentDate,
    AppointmentTime: appointmentTime,
    Status: 'Pending'
  }, (error, results) => {
    if (error) {
      throw error;
    } else {
      const transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
          user: 'youremail@gmail.com',
          pass: 'yourpassword'
        }
      });
      
      const mailOptions = {
        from: 'youremail@gmail.com',
        to: customerEmail,
        subject: 'Your Appointment Request',
        text: `Dear ${customerName}, 

Thank you for requesting an appointment with our company. We have received your request and will get back to you as soon as possible. 

Best regards, 
The Company`
      };
      
      transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
          throw error;
        } else {
          console.log(`Email sent: ${info.response}`);
        }
      });
      
      res.render('confirmation', {
        customerName,
        customerEmail,
        serviceType,
        appointmentDate,
        appointmentTime
      });
    }
  });
});

以上程式碼片段中,首先我們使用「pool.query」函數插入新的預約記錄到MySQL資料庫中,然後建立一個Nodemailer郵件傳輸器來傳送確認電子郵件給客戶。最後,我們將客戶的詳細資訊渲染到確認頁面上,以便他們可以查看他們的預約詳細資訊。

最後,我們需要使用“app.listen”函數來啟動我們的應用程序,並提供監聽口。

app.listen(3000, () => {
  console.log('Server started on port 3000');
});
  1. 啟動該應用程式

要啟動server,只需在命令提示字元或終端機中導航到程式資料夾,並執行以下命令:

node app.js

現在,在您的網頁瀏覽器中開啟“localhost:3000”,即可使用您的線上預約系統。

以上是利用Node.js實現線上預約功能的Web項目的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn