-
Notifications
You must be signed in to change notification settings - Fork 0
/
server copy.js
159 lines (136 loc) · 4.87 KB
/
server copy.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const express = require('express')
const { Sequelize, DataTypes } = require('sequelize')
const path = require('path')
const cors = require('cors')
const app = express()
app.use(express.json())
app.use(express.static(path.join(__dirname, 'static')))
app.use(cors())
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: 'database.sqlite'
})
const Post = sequelize.define('Post', {
uuid: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
title: DataTypes.STRING,
tag: DataTypes.STRING,
date: DataTypes.DATEONLY,
count: DataTypes.NUMBER,
abstract: DataTypes.STRING
})
Post.sync()
app.get('/', async (req, res) => {
res.sendFile(path.join(__dirname, './static/index.html'))
})
//创建文章
app.post('/api/posts/', async (req, res) => {
const { uuid, title, tag, date, count, abstract } = req.body
try {
const post = await Post.create({ uuid, title, tag, date, count, abstract })
res.status(201).json({ success: true, data: post })
} catch (error) {
res.status(500).json({ success: false, message: error.message })
}
})
//获取所有文章
app.get('/api/posts/', async (req, res) => {
try {
const posts = await Post.findAll()
res.send(posts)
} catch (error) {
res.status(500).json({ success: false, message: error.message })
}
})
//根据id获取具体文章
app.get('/api/posts/:uuid', async (req, res) => {
const postUUID = req.params.uuid
try {
const post = await Post.findOne({ where: { uuid: postUUID } })
if (post) {
res.send(post)
} else {
res.status(404).json({ success: false, message: 'Post not found' })
}
} catch (error) {
res.status(500).json({ success: false, message: error.message })
}
})
// 根据id更新文章
app.put('/api/posts/:uuid', async (req, res) => {
const postUUID = req.params.uuid;
const { title, tag, date, count, abstract } = req.body;
try {
// 查询文章
const post = await Post.findOne({ where: { uuid: postUUID } });
if (!post) {
return res.status(404).json({ success: false, message: 'Post not found' });
}
// 更新文章
await post.update({ title, tag, date, count, abstract });
// 返回更新后的文章
res.status(200).json({ success: true, data: post });
} catch (error) {
res.status(500).json({ success: false, message: error.message });
}
});
// 根据id删除文章
app.delete('/api/posts/:uuid', async (req, res) => {
const postUUID = req.params.uuid;
try {
const deletedCount = await Post.destroy({ where: { uuid: postUUID } });
if (deletedCount > 0) {
res.status(200).json({ success: true, message: 'Post deleted successfully' });
} else {
res.status(404).json({ success: false, message: 'Post not found' });
}
} catch (error) {
res.status(500).json({ success: false, message: error.message });
}
});
// //根据id更新文章
// app.put('/api/posts/:uuid', async (req, res) => {
// const postUUID = req.params.uuid
// const { title, tag, date, count, abstract } = req.body
// try {
// const post = await Post.findOne({ where: { uuid: postUUID } })
// if (post) {
// Post.update({
// title, tag, date, count, abstract
// }, {
// 'where': { 'uuid': postUUID }
// })
// const updated_post = await Post.findOne({ where: { uuid: postUUID } })
// res.status(200).json({ success: true, data: updated_post })
// } else {
// res.status(404).json({ success: false, message: 'Post not found' })
// }
// } catch (error) {
// res.status(500).json({ success: false, message: error.message })
// }
// })
// //根据id删除文章
// app.delete('/api/posts/:uuid', async (req, res) => {
// const postUUID = req.params.uuid
// // const { title, tag, date, count, abstract } = req.body
// try {
// const post = await Post.findOne({ where: { uuid: postUUID } })
// if (post) {
// Post.destroy({
// 'where': { 'uuid': postUUID }
// })
// const destroyed_post = await Post.findOne({ where: { uuid: postUUID } })
// res.status(200).json({ success: true, data: destroyed_post })
// } else {
// res.status(404).json({ success: false, message: 'Post not found' })
// }
// } catch (error) {
// res.status(500).json({ success: false, message: error.message })
// }
// })
app.listen(3002, () => {
console.log('http://localhost:3002')
})