Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

113 linhas
3.5 KiB

  1. /*
  2. This uses json-server, but with the module approach: https://github.com/typicode/json-server#module
  3. Downside: You can't pass the json-server command line options.
  4. Instead, can override some defaults by passing a config object to jsonServer.defaults();
  5. You have to check the source code to set some items.
  6. Examples:
  7. Validation/Customization: https://github.com/typicode/json-server/issues/266
  8. Delay: https://github.com/typicode/json-server/issues/534
  9. ID: https://github.com/typicode/json-server/issues/613#issuecomment-325393041
  10. Relevant source code: https://github.com/typicode/json-server/blob/master/src/cli/run.js
  11. */
  12. /* eslint-disable no-console */
  13. const jsonServer = require("json-server");
  14. const server = jsonServer.create();
  15. const path = require("path");
  16. const router = jsonServer.router(path.join(__dirname, "db.json"));
  17. // Can pass a limited number of options to this to override (some) defaults. See https://github.com/typicode/json-server#api
  18. const middlewares = jsonServer.defaults({
  19. // Display json-server's built in homepage when json-server starts.
  20. static: "node_modules/json-server/dist",
  21. });
  22. // Set default middlewares (logger, static, cors and no-cache)
  23. server.use(middlewares);
  24. // To handle POST, PUT and PATCH you need to use a body-parser. Using JSON Server's bodyParser
  25. server.use(jsonServer.bodyParser);
  26. // Simulate delay on all requests
  27. server.use(function (req, res, next) {
  28. setTimeout(next, 0);
  29. });
  30. // Declaring custom routes below. Add custom routes before JSON Server router
  31. // Add createdAt to all POSTS
  32. server.use((req, res, next) => {
  33. if (req.method === "POST") {
  34. req.body.createdAt = Date.now();
  35. }
  36. // Continue to JSON Server router
  37. next();
  38. });
  39. server.post("/courses/", function (req, res, next) {
  40. const error = validateCourse(req.body);
  41. if (error) {
  42. res.status(400).send(error);
  43. } else {
  44. req.body.slug = createSlug(req.body.title); // Generate a slug for new courses.
  45. next();
  46. }
  47. });
  48. server.post("/users/", function (req, res, next) {
  49. const error = validateUser(req.body);
  50. if (error) {
  51. res.status(400).send(error);
  52. } else {
  53. req.body.slug = createSlug(req.body.title); // Generate a slug for new users.
  54. next();
  55. }
  56. });
  57. /***********************************************
  58. * bpapiServerSnipppet at the above this line *
  59. * There will be 2 tab stops that need changed *
  60. ***********************************************
  61. */
  62. // Use default router
  63. server.use(router);
  64. // Start server
  65. const port = 3001;
  66. server.listen(port, () => {
  67. console.log(`JSON Server is running on port ${port}`);
  68. });
  69. // Centralized logic
  70. // Returns a URL friendly slug
  71. function createSlug(value) {
  72. return value
  73. .replace(/[^a-z0-9_]+/gi, "-")
  74. .replace(/^-|-$/g, "")
  75. .toLowerCase();
  76. }
  77. function validateCourse(course) {
  78. if (!course.title) return "Title is required.";
  79. if (!course.authorId) return "Author is required.";
  80. if (!course.category) return "Category is required.";
  81. return "";
  82. }
  83. function validateUser(user) {
  84. if (!user.firstname) return "First name is required.";
  85. if (!user.lastname) return "Last name is required.";
  86. if (!user.username) return "Username is required.";
  87. if (!user.password) return "Password is required.";
  88. if (!user.emil) return "Email is required.";
  89. if (!user.isactive) return "Is active is required.";
  90. return "";
  91. }
  92. /***********************************************
  93. * bpapiServerValidation at the above this line*
  94. * There will be 2 tab stops that need changed *
  95. ***********************************************
  96. */