├── README.md ├── package.json ├── Rest.txt └── app.js /README.md: -------------------------------------------------------------------------------- 1 | # REST-wiki_Api 2 | 3 | ![image](https://user-images.githubusercontent.com/107684179/187181699-637ae746-51d9-4541-af67-8905f9f986fe.png) 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "body-parser": "^1.20.0", 4 | "ejs": "^3.1.8", 5 | "express": "^4.18.1", 6 | "mongoose": "^6.5.3" 7 | }, 8 | "name": "wiki-api", 9 | "version": "1.0.0", 10 | "main": "app.js", 11 | "devDependencies": {}, 12 | "scripts": { 13 | "test": "echo \"Error: no test specified\" && exit 1" 14 | }, 15 | "keywords": [], 16 | "author": "", 17 | "license": "ISC", 18 | "description": "" 19 | } 20 | -------------------------------------------------------------------------------- /Rest.txt: -------------------------------------------------------------------------------- 1 | C R U D 2 | Get = Read 3 | Post = Create 4 | Put(new) / Patch(repair) = Update 5 | Delete = Delete 6 | 7 | 8 | 9 | - Robo 3T 10 | https://studio3t.com/ 11 | download n install n connect 12 | mongod 13 | > show dbs 14 | 15 | - Robo 3T 16 | localhost:27017 17 | - add databases 18 | - create wikiDB 19 | *Collections 20 | - add collections 21 | - articles 22 | - Open Collenction Ttab 23 | - 在articles的中间add documents 24 | { 25 | title: 'REST', 26 | content: 'REST is short for REpresentational State Transfer. It an architecthral style for designing APIs.' 27 | } 28 | ************************************** 29 | mkidr wiki-api 30 | npm init -y 31 | npm i body-parser mongoose ejs express 32 | touch app.js 33 | nodemon app.js 34 | 35 | 36 | 37 | // 366 38 | - Read 39 | .find({conditions}, function(err, results){ 40 | //use the found results docs. 41 | }); 42 | 43 | 44 | 45 | // 367 46 | - Post 47 | app.post(route, function(req, res){ 48 | }); 49 | 50 | - Postman 51 | Get change to Post - localhost:3000/articles 52 | - click Body 53 | - click x-www-form-urlencoded 54 | KEY 55 | - title 56 | - content 57 | 58 | - Create 59 | const = new ({ 60 | : , 61 | ... 62 | }); 63 | .save(); 64 | 65 | 66 | 67 | // 368 68 | - Delete 69 | app.delete(route, function(req, res){ 70 | }); 71 | 72 | 73 | - Delete 74 | .deleteMany( 75 | {conditions}, 76 | function(err){ 77 | } 78 | ); 79 | 80 | - Postman 81 | Post change to Get - localhost:3000/articles 82 | - click Body 83 | Get change to Delete 84 | check Body and Robo 3T, all detail deleted. 85 | 86 | 87 | 88 | // 370 89 | - Get 90 | app.get(route, function(req, res){ 91 | }) 92 | 93 | - Get 94 | app.route('route') 95 | .get(function(req, res){ 96 | }) 97 | 98 | - Read 99 | .findOne( 100 | {conditions}, 101 | function(err, result){ 102 | //use the found result. 103 | } 104 | ); 105 | 106 | - Robo 3T 107 | localhost:27017 108 | - add databases 109 | - create wikiDB 110 | *Collections 111 | - add collections 112 | - articles 113 | - Open Collenction Ttab 114 | - 在articles的中间add documents 115 | { 116 | "_id" : "5c18f35cde40ab6cc551cd60", 117 | "title" : "Jack Bauer", 118 | "content" : "Jack Bauer once stepped into quicksand. The quicksand couldn't escape and nearly drowned.", 119 | "__v" : 0 120 | } 121 | 122 | - Postman 123 | Post change to Get - localhost:3000/articles 124 | - click Body 125 | 126 | 127 | 128 | // 371 129 | - Post 130 | app.put(route, function(req, res){ 131 | }) 132 | 133 | - Update 134 | .update( 135 | {conditions}, 136 | {updates}, 137 | {overwrite: true} 138 | function(err, results){} 139 | ); 140 | 141 | - Postman 142 | Get change to Put - localhost:3000/articles/Jack%20Bauer 143 | - click Body 144 | 145 | 146 | 147 | // 372 148 | - Patch 149 | app.patch(route, function(req, res){ 150 | }) 151 | 152 | - Update 153 | .update( 154 | {conditions}, 155 | {$set: updates}, 156 | function(err, results){} 157 | ); 158 | 159 | - Postman 160 | Put change to Patch - localhost:3000/articles/Jack%20Bauer 161 | - click Body 162 | 163 | 164 | 165 | // 373 166 | - Delete 167 | app.delete(route, function(req, res){ 168 | }) 169 | 170 | - Delete 171 | .deleteOne( 172 | {conditions}, 173 | function(err){} 174 | ); 175 | 176 | - Postman 177 | Patch change to Delete - localhost:3000/articles/Jack%20Bauer 178 | - click Body 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | //jshint esversion:6 2 | // 365 3 | // const express = require("express"); 4 | // const bodyParser = require("body-parser"); 5 | // const ejs = require("ejs"); 6 | // const mongoose = require('mongoose'); 7 | 8 | // const app = express(); 9 | 10 | // app.set('view engine', 'ejs'); 11 | 12 | // app.use(bodyParser.urlencoded({ 13 | // extended: true 14 | // })); 15 | // app.use(express.static("public")); 16 | 17 | // mongoose.connect('mongodb://localhost:27017/wikiDB', {userNewUrlParser: true}); 18 | 19 | // const articleSchema = { 20 | // title: String, 21 | // content: String 22 | // }; 23 | 24 | // const Article = mongoose.model('Article', articleSchema); 25 | 26 | // //TODO 27 | 28 | // app.listen(3000, function() { 29 | // console.log("Server started on port 3000"); 30 | // }); 31 | 32 | // nodemon app.js 33 | 34 | 35 | 36 | // 366 37 | // const express = require("express"); 38 | // const bodyParser = require("body-parser"); 39 | // const ejs = require("ejs"); 40 | // const mongoose = require('mongoose'); 41 | 42 | // const app = express(); 43 | 44 | // app.set('view engine', 'ejs'); 45 | 46 | // app.use(bodyParser.urlencoded({ 47 | // extended: true 48 | // })); 49 | // app.use(express.static("public")); 50 | 51 | // mongoose.connect('mongodb://localhost:27017/wikiDB', {useNewUrlParser: true}); 52 | 53 | // const articleSchema = { 54 | // title: String, 55 | // content: String 56 | // }; 57 | 58 | // const Article = mongoose.model('Article', articleSchema); 59 | 60 | // app.get('/articles', function(req, res){ 61 | // Article.find(function(err, foundArticles){ 62 | // if (!err) { 63 | // res.send(foundArticles); 64 | // } else { 65 | // res.send(err); 66 | // } 67 | 68 | // }); 69 | // }); 70 | 71 | // app.post('/articles', function(req, res){ 72 | // console.log(req.body.title); 73 | // console.log(req.body.content); 74 | 75 | // const newArticle = new Article({ 76 | // title: req.body.title, 77 | // content: req.body.content 78 | // }); 79 | 80 | // newArticle.save(function(err){ 81 | // if (!err){ 82 | // res.send('successfully added a new article.'); 83 | // } else { 84 | // res.send(err); 85 | // } 86 | // }); 87 | // }); 88 | 89 | // app.listen(3000, function() { 90 | // console.log("Server started on port 3000"); 91 | // }); 92 | 93 | // nodemon app.js 94 | 95 | 96 | 97 | // 368 98 | // const express = require("express"); 99 | // const bodyParser = require("body-parser"); 100 | // const ejs = require("ejs"); 101 | // const mongoose = require('mongoose'); 102 | 103 | // const app = express(); 104 | 105 | // app.set('view engine', 'ejs'); 106 | 107 | // app.use(bodyParser.urlencoded({ 108 | // extended: true 109 | // })); 110 | // app.use(express.static("public")); 111 | 112 | // mongoose.connect('mongodb://localhost:27017/wikiDB', {useNewUrlParser: true}); 113 | 114 | // const articleSchema = { 115 | // title: String, 116 | // content: String 117 | // }; 118 | 119 | // const Article = mongoose.model('Article', articleSchema); 120 | 121 | // app.get('/articles', function(req, res){ 122 | // Article.find(function(err, foundArticles){ 123 | // if (!err) { 124 | // res.send(foundArticles); 125 | // } else { 126 | // res.send(err); 127 | // } 128 | 129 | // }); 130 | // }); 131 | 132 | // app.post('/articles', function(req, res){ 133 | // console.log(req.body.title); 134 | // console.log(req.body.content); 135 | 136 | // const newArticle = new Article({ 137 | // title: req.body.title, 138 | // content: req.body.content 139 | // }); 140 | 141 | // newArticle.save(function(err){ 142 | // if (!err){ 143 | // res.send('successfully added a new article.'); 144 | // } else { 145 | // res.send(err); 146 | // } 147 | // }); 148 | // }); 149 | 150 | // app.delete('/articles', function(req, res){ 151 | // Article.deleteMany(function(err){ 152 | // if (!err){ 153 | // res.send('successfully deleted all articles.') 154 | // } else { 155 | // res.send(err); 156 | // } 157 | // }); 158 | // }); 159 | 160 | // app.listen(3000, function() { 161 | // console.log("Server started on port 3000"); 162 | // }); 163 | 164 | // nodemon app.js 165 | 166 | 167 | 168 | // 369 169 | // const express = require("express"); 170 | // const bodyParser = require("body-parser"); 171 | // const ejs = require("ejs"); 172 | // const mongoose = require('mongoose'); 173 | 174 | // const app = express(); 175 | 176 | // app.set('view engine', 'ejs'); 177 | 178 | // app.use(bodyParser.urlencoded({ 179 | // extended: true 180 | // })); 181 | // app.use(express.static("public")); 182 | 183 | // mongoose.connect('mongodb://localhost:27017/wikiDB', {useNewUrlParser: true}); 184 | 185 | // const articleSchema = { 186 | // title: String, 187 | // content: String 188 | // }; 189 | 190 | // const Article = mongoose.model('Article', articleSchema); 191 | 192 | // app.route('/articles') 193 | 194 | // .get(function(req, res){ 195 | // Article.find(function(err, foundArticles){ 196 | // if (!err) { 197 | // res.send(foundArticles); 198 | // } else { 199 | // res.send(err); 200 | // } 201 | 202 | // }); 203 | // }) 204 | 205 | // .post(function(req, res){ 206 | // console.log(req.body.title); 207 | // console.log(req.body.content); 208 | 209 | // const newArticle = new Article({ 210 | // title: req.body.title, 211 | // content: req.body.content 212 | // }); 213 | 214 | // newArticle.save(function(err){ 215 | // if (!err){ 216 | // res.send('successfully added a new article.'); 217 | // } else { 218 | // res.send(err); 219 | // } 220 | // }); 221 | // }) 222 | 223 | // .delete(function(req, res){ 224 | // Article.deleteMany(function(err){ 225 | // if (!err){ 226 | // res.send('successfully deleted all articles.') 227 | // } else { 228 | // res.send(err); 229 | // } 230 | // }); 231 | // }); 232 | 233 | 234 | 235 | // app.listen(3000, function() { 236 | // console.log("Server started on port 3000"); 237 | // }); 238 | 239 | // nodemon app.js 240 | 241 | 242 | 243 | // 370 244 | // const express = require("express"); 245 | // const bodyParser = require("body-parser"); 246 | // const ejs = require("ejs"); 247 | // const mongoose = require('mongoose'); 248 | 249 | // const app = express(); 250 | 251 | // app.set('view engine', 'ejs'); 252 | 253 | // app.use(bodyParser.urlencoded({ 254 | // extended: true 255 | // })); 256 | // app.use(express.static("public")); 257 | 258 | // mongoose.connect('mongodb://localhost:27017/wikiDB', {useNewUrlParser: true}); 259 | 260 | // const articleSchema = { 261 | // title: String, 262 | // content: String 263 | // }; 264 | 265 | // const Article = mongoose.model('Article', articleSchema); 266 | 267 | // ////////////////////////////////////Requests Targetting all Articles///////////////////// 268 | 269 | // app.route('/articles') 270 | 271 | // .get(function(req, res){ 272 | // Article.find(function(err, foundArticles){ 273 | // if (!err) { 274 | // res.send(foundArticles); 275 | // } else { 276 | // res.send(err); 277 | // } 278 | 279 | // }); 280 | // }) 281 | 282 | // .post(function(req, res){ 283 | // console.log(req.body.title); 284 | // console.log(req.body.content); 285 | 286 | // const newArticle = new Article({ 287 | // title: req.body.title, 288 | // content: req.body.content 289 | // }); 290 | 291 | // newArticle.save(function(err){ 292 | // if (!err){ 293 | // res.send('successfully added a new article.'); 294 | // } else { 295 | // res.send(err); 296 | // } 297 | // }); 298 | // }) 299 | 300 | // .delete(function(req, res){ 301 | // Article.deleteMany(function(err){ 302 | // if (!err){ 303 | // res.send('successfully deleted all articles.') 304 | // } else { 305 | // res.send(err); 306 | // } 307 | // }); 308 | // }); 309 | 310 | // ////////////////////////////////////Requests Targetting A Specific Articles///////////////////// 311 | 312 | // app.route('/articles/:articleTitle') 313 | 314 | // .get(function(req, res){ 315 | 316 | // Article.findOne({title: req.params.articleTitle}, function(err, foundArticles){ 317 | // if (foundArticles) { 318 | // res.send(foundArticles); 319 | // } else { 320 | // res.send('no articles matching that title was found.'); 321 | // } 322 | // }); 323 | // }); 324 | 325 | // app.listen(3000, function() { 326 | // console.log("Server started on port 3000"); 327 | // }); 328 | 329 | // nodemon app.js 330 | 331 | 332 | 333 | // 371 334 | // const express = require("express"); 335 | // const bodyParser = require("body-parser"); 336 | // const ejs = require("ejs"); 337 | // const mongoose = require('mongoose'); 338 | 339 | // const app = express(); 340 | 341 | // app.set('view engine', 'ejs'); 342 | 343 | // app.use(bodyParser.urlencoded({ 344 | // extended: true 345 | // })); 346 | // app.use(express.static("public")); 347 | 348 | // mongoose.connect('mongodb://localhost:27017/wikiDB', {useNewUrlParser: true}); 349 | 350 | // const articleSchema = { 351 | // title: String, 352 | // content: String 353 | // }; 354 | 355 | // const Article = mongoose.model('Article', articleSchema); 356 | 357 | // ////////////////////////////////////Requests Targetting all Articles///////////////////// 358 | 359 | // app.route('/articles') 360 | 361 | // .get(function(req, res){ 362 | // Article.find(function(err, foundArticles){ 363 | // if (!err) { 364 | // res.send(foundArticles); 365 | // } else { 366 | // res.send(err); 367 | // } 368 | 369 | // }); 370 | // }) 371 | 372 | // .post(function(req, res){ 373 | // console.log(req.body.title); 374 | // console.log(req.body.content); 375 | 376 | // const newArticle = new Article({ 377 | // title: req.body.title, 378 | // content: req.body.content 379 | // }); 380 | 381 | // newArticle.save(function(err){ 382 | // if (!err){ 383 | // res.send('successfully added a new article.'); 384 | // } else { 385 | // res.send(err); 386 | // } 387 | // }); 388 | // }) 389 | 390 | // .delete(function(req, res){ 391 | // Article.deleteMany(function(err){ 392 | // if (!err){ 393 | // res.send('successfully deleted all articles.') 394 | // } else { 395 | // res.send(err); 396 | // } 397 | // }); 398 | // }); 399 | 400 | // ////////////////////////////////////Requests Targetting A Specific Articles///////////////////// 401 | 402 | // app.route('/articles/:articleTitle') 403 | 404 | // .get(function(req, res){ 405 | 406 | // Article.findOne({title: req.params.articleTitle}, function(err, foundArticles){ 407 | // if (foundArticles) { 408 | // res.send(foundArticles); 409 | // } else { 410 | // res.send('no articles matching that title was found.'); 411 | // } 412 | // }); 413 | // }) 414 | 415 | // .put(function(req, res){ 416 | // Article.update( 417 | // {title: req.params.articleTitle}, 418 | // {title: req.body.title, content: req.body.content}, 419 | // {overwrite: true}, 420 | // function(err){ 421 | // if(!err){ 422 | // res.send('successfully updated article.'); 423 | // } 424 | // } 425 | // ); 426 | // }); 427 | 428 | // app.listen(3000, function() { 429 | // console.log("Server started on port 3000"); 430 | // }); 431 | 432 | // nodemon app.js 433 | 434 | 435 | 436 | // 372 437 | // const express = require("express"); 438 | // const bodyParser = require("body-parser"); 439 | // const ejs = require("ejs"); 440 | // const mongoose = require('mongoose'); 441 | 442 | // const app = express(); 443 | 444 | // app.set('view engine', 'ejs'); 445 | 446 | // app.use(bodyParser.urlencoded({ 447 | // extended: true 448 | // })); 449 | // app.use(express.static("public")); 450 | 451 | // mongoose.connect('mongodb://localhost:27017/wikiDB', {useNewUrlParser: true}); 452 | 453 | // const articleSchema = { 454 | // title: String, 455 | // content: String 456 | // }; 457 | 458 | // const Article = mongoose.model('Article', articleSchema); 459 | 460 | // ////////////////////////////////////Requests Targetting all Articles///////////////////// 461 | 462 | // app.route('/articles') 463 | 464 | // .get(function(req, res){ 465 | // Article.find(function(err, foundArticles){ 466 | // if (!err) { 467 | // res.send(foundArticles); 468 | // } else { 469 | // res.send(err); 470 | // } 471 | 472 | // }); 473 | // }) 474 | 475 | // .post(function(req, res){ 476 | // console.log(req.body.title); 477 | // console.log(req.body.content); 478 | 479 | // const newArticle = new Article({ 480 | // title: req.body.title, 481 | // content: req.body.content 482 | // }); 483 | 484 | // newArticle.save(function(err){ 485 | // if (!err){ 486 | // res.send('successfully added a new article.'); 487 | // } else { 488 | // res.send(err); 489 | // } 490 | // }); 491 | // }) 492 | 493 | // .delete(function(req, res){ 494 | // Article.deleteMany(function(err){ 495 | // if (!err){ 496 | // res.send('successfully deleted all articles.') 497 | // } else { 498 | // res.send(err); 499 | // } 500 | // }); 501 | // }); 502 | 503 | // ////////////////////////////////////Requests Targetting A Specific Articles///////////////////// 504 | 505 | // app.route('/articles/:articleTitle') 506 | 507 | // .get(function(req, res){ 508 | 509 | // Article.findOne({title: req.params.articleTitle}, function(err, foundArticles){ 510 | // if (foundArticles) { 511 | // res.send(foundArticles); 512 | // } else { 513 | // res.send('no articles matching that title was found.'); 514 | // } 515 | // }); 516 | // }) 517 | 518 | // .put(function(req, res){ 519 | // Article.update( 520 | // {title: req.params.articleTitle}, 521 | // {title: req.body.title, content: req.body.content}, 522 | // {overwrite: true}, 523 | // function(err){ 524 | // if(!err){ 525 | // res.send('successfully updated article.'); 526 | // } 527 | // } 528 | // ); 529 | // }) 530 | 531 | // .patch(function(req, res){ 532 | // Article.update( 533 | // {title: req.params.articleTitle}, 534 | // {$set: req.body}, 535 | // function(err){ 536 | // if(!err){ 537 | // res.send('successfully updated article.') 538 | // } else { 539 | // res.send(err); 540 | // } 541 | // } 542 | // ); 543 | // }); 544 | 545 | // app.listen(3000, function() { 546 | // console.log("Server started on port 3000"); 547 | // }); 548 | 549 | // nodemon app.js 550 | 551 | 552 | 553 | // 373 554 | const express = require("express"); 555 | const bodyParser = require("body-parser"); 556 | const ejs = require("ejs"); 557 | const mongoose = require('mongoose'); 558 | 559 | const app = express(); 560 | 561 | app.set('view engine', 'ejs'); 562 | 563 | app.use(bodyParser.urlencoded({ 564 | extended: true 565 | })); 566 | app.use(express.static("public")); 567 | 568 | mongoose.connect('mongodb://localhost:27017/wikiDB', {useNewUrlParser: true}); 569 | 570 | const articleSchema = { 571 | title: String, 572 | content: String 573 | }; 574 | 575 | const Article = mongoose.model('Article', articleSchema); 576 | 577 | ////////////////////////////////////Requests Targetting all Articles///////////////////// 578 | 579 | app.route('/articles') 580 | 581 | .get(function(req, res){ 582 | Article.find(function(err, foundArticles){ 583 | if (!err) { 584 | res.send(foundArticles); 585 | } else { 586 | res.send(err); 587 | } 588 | 589 | }); 590 | }) 591 | 592 | .post(function(req, res){ 593 | console.log(req.body.title); 594 | console.log(req.body.content); 595 | 596 | const newArticle = new Article({ 597 | title: req.body.title, 598 | content: req.body.content 599 | }); 600 | 601 | newArticle.save(function(err){ 602 | if (!err){ 603 | res.send('successfully added a new article.'); 604 | } else { 605 | res.send(err); 606 | } 607 | }); 608 | }) 609 | 610 | .delete(function(req, res){ 611 | Article.deleteMany(function(err){ 612 | if (!err){ 613 | res.send('successfully deleted all articles.') 614 | } else { 615 | res.send(err); 616 | } 617 | }); 618 | }); 619 | 620 | ////////////////////////////////////Requests Targetting A Specific Articles///////////////////// 621 | 622 | app.route('/articles/:articleTitle') 623 | 624 | .get(function(req, res){ 625 | 626 | Article.findOne({title: req.params.articleTitle}, function(err, foundArticles){ 627 | if (foundArticles) { 628 | res.send(foundArticles); 629 | } else { 630 | res.send('no articles matching that title was found.'); 631 | } 632 | }); 633 | }) 634 | 635 | .put(function(req, res){ 636 | Article.update( 637 | {title: req.params.articleTitle}, 638 | {title: req.body.title, content: req.body.content}, 639 | {overwrite: true}, 640 | function(err){ 641 | if(!err){ 642 | res.send('successfully updated article.'); 643 | } 644 | } 645 | ); 646 | }) 647 | 648 | .patch(function(req, res){ 649 | Article.update( 650 | {title: req.params.articleTitle}, 651 | {$set: req.body}, 652 | function(err){ 653 | if(!err){ 654 | res.send('successfully updated article.') 655 | } else { 656 | res.send(err); 657 | } 658 | } 659 | ); 660 | }) 661 | 662 | .delete(function(req, res){ 663 | Article.deleteOne( 664 | {title: req.params.articleTitle}, 665 | function(err){ 666 | if (!err){ 667 | res.send('successfully deleted the corresponding article.'); 668 | } else { 669 | res.send(err); 670 | } 671 | } 672 | ); 673 | }); 674 | 675 | app.listen(3000, function() { 676 | console.log("Server started on port 3000"); 677 | }); 678 | 679 | // nodemon app.js --------------------------------------------------------------------------------