TypeScript 语言 实战项目 在线考试系统

TypeScriptamuwap 发布于 15 小时前 1 次阅读


在线考试系统【1】实战项目:TypeScript【2】语言下的代码实现

随着互联网技术的飞速发展,在线教育【3】逐渐成为教育行业的新趋势。在线考试系统作为在线教育的重要组成部分,能够有效提高考试效率和公平性。本文将围绕TypeScript语言,详细介绍一个在线考试系统的实战项目,包括系统设计、技术选型、核心代码实现等方面。

一、系统设计

1. 系统架构

在线考试系统采用前后端分离【4】的架构,前端使用TypeScript编写,后端使用Node.js【5】。系统分为以下几个模块:

- 用户模块:负责用户注册、登录、权限管理【6】等功能。
- 题库【7】模块:负责题目的增删改查、分类管理等功能。
- 考试模块:负责考试流程管理【8】、成绩统计【9】等功能。
- 系统管理【10】模块:负责系统配置【11】、日志管理等。

2. 技术选型

- 前端:TypeScript、Vue.js、Element UI
- 后端:Node.js、Express【12】、Mongoose【13】
- 数据库:MongoDB【14】
- 其他:JWT【15】(JSON Web Token)、Redis

二、核心代码实现

1. 用户模块

用户注册

typescript
// User.ts
import { Schema, Document } from 'mongoose';
import as bcrypt from 'bcrypt';

export interface IUser extends Document {
username: string;
password: string;
email: string;
}

const UserSchema: Schema = new Schema({
username: { type: String, required: true },
password: { type: String, required: true },
email: { type: String, required: true }
});

UserSchema.pre('save', async function(next) {
if (this.isModified('password')) {
this.password = await bcrypt.hash(this.password, 10);
}
next();
});

export default mongoose.model('User', UserSchema);

用户登录

typescript
// authController.ts
import { Request, Response } from 'express';
import { User } from '../models/User';
import as bcrypt from 'bcrypt';
import as jwt from 'jsonwebtoken';

export const login = async (req: Request, res: Response) => {
const { username, password } = req.body;
const user = await User.findOne({ username });
if (!user) {
return res.status(404).send('User not found');
}
const validPassword = await bcrypt.compare(password, user.password);
if (!validPassword) {
return res.status(400).send('Invalid password');
}
const token = jwt.sign({ _id: user._id }, 'secretKey');
res.send({ token });
};

2. 题库模块

题目添加

typescript
// Question.ts
import { Schema, Document } from 'mongoose';

export interface IQuestion extends Document {
title: string;
options: string[];
answer: string;
category: string;
}

const QuestionSchema: Schema = new Schema({
title: { type: String, required: true },
options: { type: [String], required: true },
answer: { type: String, required: true },
category: { type: String, required: true }
});

export default mongoose.model('Question', QuestionSchema);

题目查询

typescript
// questionController.ts
import { Request, Response } from 'express';
import { Question } from '../models/Question';

export const getQuestions = async (req: Request, res: Response) => {
const questions = await Question.find();
res.send(questions);
};

3. 考试模块

考试开始

typescript
// examController.ts
import { Request, Response } from 'express';
import { Question } from '../models/Question';
import { Exam } from '../models/Exam';

export const startExam = async (req: Request, res: Response) => {
const questions = await Question.aggregate([{ $sample: { size: 10 } }]);
const exam = new Exam({
questions,
startTime: new Date(),
endTime: new Date(new Date().getTime() + 3600000) // 1小时后结束
});
await exam.save();
res.send(exam);
};

考试提交

typescript
// examController.ts
import { Request, Response } from 'express';
import { Exam } from '../models/Exam';

export const submitExam = async (req: Request, res: Response) => {
const { examId, answers } = req.body;
const exam = await Exam.findById(examId);
if (!exam) {
return res.status(404).send('Exam not found');
}
exam.endTime = new Date();
exam.results = answers;
await exam.save();
res.send('Exam submitted successfully');
};

4. 系统管理模块

系统配置

typescript
// systemController.ts
import { Request, Response } from 'express';

export const updateConfig = async (req: Request, res: Response) => {
const { key, value } = req.body;
// 更新系统配置
res.send('System configuration updated successfully');
};

三、总结

本文以TypeScript语言为基础,详细介绍了在线考试系统的实战项目。通过前后端分离的架构,实现了用户模块、题库模块、考试模块和系统管理模块等功能。在实际开发过程中,可以根据需求进行扩展和优化。希望本文能对您在TypeScript语言下的在线考试系统开发有所帮助。