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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
| const http = require('http')
const app = http.createServer() const url = require('url') const mongoose = require('mongoose')
const querystring = require('querystring')
mongoose.connect('mongodb://localhost/playground', { useUnifiedTopology: true, useNewUrlParser: true }) .then(() => console.log('成功')) .catch(() => console.log('失败'))
const UserSchema = new mongoose.Schema({ name: { type: String, resquired: true, minlength: 2, maxlength: 20 }, age: { type: Number, min: 3, max: 80 }, password: String, email: String, hobbies: [String] })
const user = mongoose.model('User', UserSchema)
app.on('request', async (req, res) => { const method = req.method const { pathname, query} = url.parse(req.url,true) if (method == 'GET') { if (pathname == '/list' || pathname == '/') { let Users = await user.find() let list = `<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>列表页</title> <script src="https://cdn.jsdelivr.net/gh/Rr210/[email protected]/js/jquery-3.5.1.min.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Rr210/hexofixed@frame/frame/bootstrap/3/css/bootstrap.min.css"> <script src="https://cdn.jsdelivr.net/gh/Rr210/hexofixed@frame/frame/bootstrap/3/js/bootstrap.min.js"></script> <style> *{ padding: 0; margin: 0; } .addd{ display:inline-block; margin: 20px auto; } a{ margin:10px } .table { margin: 10px auto; width: 70%; } td span{ margin: 0 5px; } </style> </head> <body> <a class="btn btn-success addd" href="/add">添加用户</a> <table class="table table-striped table-bordered"> <thead> <tr> <th>用户名</th> <th>年龄</th> <th>爱好</th> <th>邮箱</th> <th>操作</th> </tr> </thead> <tbody>` Users.forEach(item => { list += `<tr> <td>${item.name}</td> <td>${item.age}</td><td>` item.hobbies.forEach(item => { list += `<span>${item}</span>` }) list += `</td><td>${item.email}</td><td><a class="btn btn-primary" href="/modify?id=${item._id}">修改</a><a class="btn btn-danger" href="/remove?id=${item._id}">删除</a></td> </tr>` }) list += ` </tbody> </table> <script> // css的修改 获取元素 window.addEventListener('DOMContentLoaded',function(){ // 构造函数 let resize = function(){ var adduser = document.querySelector('.addd') var tablebox = document.querySelector('table') // console.log(tablebox.offsetLeft); adduser.style.marginLeft = tablebox.offsetLeft + 'px' } // 当页面加载后执行一次,并且窗口改变时跟随变化 resize(); window.addEventListener('resize', () => { var adduser = document.querySelector('.addd') resize() }, false); }) </script> </body> </html>` res.end(list) }else if(pathname == '/add'){ let add = `<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>添加用户</title> <script src="https://cdn.jsdelivr.net/gh/Rr210/[email protected]/js/jquery-3.5.1.min.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Rr210/hexofixed@frame/frame/bootstrap/3/css/bootstrap.min.css"> <script src="https://cdn.jsdelivr.net/gh/Rr210/hexofixed@frame/frame/bootstrap/3/js/bootstrap.min.js"></script> </head> <style> * { padding: 0; margin: 0; } body { background-color: #ccc; } .box { width: 770px; margin: 30px auto 0; border: 2px solid #888; border-radius: 10px; } h5,button{ margin: 20px auto; text-align: center; font-size: 20px; font-weight: 600; } .btn{ display: block; margin-bottom: 10px; } .form-control { width: 93%; margin: auto; } label { margin-left: 30px; margin-right: 7px; } .hobb{ display: block; } </style> <body> <div class="box"> <h5>添加用户</h5> <form method="POST" action="/add"> <div class="form-group"> <label for="username">User Name</label> <input name="name" type="text" class="form-control" id="username" placeholder="请填写用户名"> </div> <div class="form-group"> <label for="mima">Password</label> <input name="password" type="password" class="form-control" id="mima" placeholder="Password"> </div> <div class="form-group"> <label for="exampleInputFile">Age</label> <input name="age" type="number" id="exampleInputFile" placeholder="请输入年龄" class="form-control"> </div> <div class="form-group"> <label for="exampleInputemail">Age</label> <input name="email" type="email" id="exampleInputemail" placeholder="请输入邮箱" class="form-control"> </div> <div class="form-group"> <label for="" class="hobb">hobbies</label> <label for="football"> 足球</label><input type="checkbox" id="football" name="hobbies" value="足球"> <label for="baseball"> 篮球</label><input type="checkbox" id="baseball" name="hobbies"value="篮球"> <label for="code"> 敲代码</label><input type="checkbox" id="code" name="hobbies"value="敲代码"> <label for="book"> 看书</label><input type="checkbox" id="book" name="hobbies"value="看书"> </div> <button type="submit" class="btn btn-primary">Add User</button> </form> </div> </body> </html>` res.end(add) }else if(pathname == '/modify'){ let userid = await user.findOne({_id:query.id}) let hobbiess = ['足球','篮球','羽毛球','学习','看书','敲代码'] console.log(userid); let modify = `<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>修改用户信息</title> <script src="https://cdn.jsdelivr.net/gh/Rr210/[email protected]/js/jquery-3.5.1.min.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Rr210/hexofixed@frame/frame/bootstrap/3/css/bootstrap.min.css"> <script src="https://cdn.jsdelivr.net/gh/Rr210/hexofixed@frame/frame/bootstrap/3/js/bootstrap.min.js"></script> </head> <style> * { padding: 0; margin: 0; } body { background-color: #ccc; } .box { width: 770px; margin: 30px auto 0; border: 2px solid #888; border-radius: 10px; } h5,button{ margin: 20px auto; text-align: center; font-size: 20px; font-weight: 600; } .btn{ display: block; margin-bottom: 10px; } .form-control { width: 93%; margin: auto; } label { margin-left: 30px; margin-right: 7px; } .hobb{ display: block; } </style> <body> <div class="box"> <h5>修改用户信息</h5> <form method="POST" action="/modify?id=${userid._id}"> <div class="form-group"> <label for="username">User Name</label> <input value="${userid.name}" name="name" type="text" class="form-control" id="username" placeholder="请填写用户名"> </div> <div class="form-group"> <label for="mima">Password</label> <input value="${userid.password}" name="password" type="password" class="form-control" id="mima" placeholder="Password"> </div> <div class="form-group"> <label for="exampleInputFile">Age</label> <input value="${userid.age}" name="age" type="number" id="exampleInputFile" placeholder="请输入年龄" class="form-control"> </div> <div class="form-group"> <label for="exampleInputemail">Age</label> <input value="${userid.email}" name="email" type="email" id="exampleInputemail" placeholder="请输入邮箱" class="form-control"> </div> <div class="form-group"> <label for="" class="hobb">hobbies</label>` hobbiess.forEach(item=>{ let Hobbies = userid.hobbies.includes(item) if(Hobbies){ modify += ` <label for="football">${item}</label><input type="checkbox" id="football" name="hobbies" value="${item}" checked>` }else{ modify += ` <label for="football">${item}</label><input type="checkbox" id="football" name="hobbies" value="${item}">` } }) modify += ` </div> <button type="submit" class="btn btn-primary">Add User</button> </form> </div> </body> </html>` res.end(modify) }else if(pathname == '/remove'){ await user.findOneAndDelete({_id:query.id}) res.writeHead(301,{ location:'/list' }) res.end() } } else if (method == "POST") { let formData = '' if(pathname == '/add'){ req.on('data',param=>{ formData += param }) req.on('end',async () =>{ let users = querystring.parse(formData) await user.create(users) res.writeHead(301,{ location:'/list' }); res.end() }) }else if(pathname == '/modify'){ req.on('data',param=>{ formData += param }) req.on('end',async () =>{ let users = querystring.parse(formData) await user.updateOne({_id:query.id},users) res.writeHead(301,{ location:'/list' }); res.end() }) } } })
app.listen(3100)
|