SHARE
TWEET

Untitled

a guest Jun 26th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require("express");
  2. const path = require("path");
  3. const bodyParser = require("body-parser");
  4. const { check } = require("express-validator/check");
  5. const serverPort = process.env.PORT || 8888;
  6. const authentication = require("./authentication.js");
  7.  
  8. /* Controllers */
  9. const announceCtrl = require("./controllers/announceController.js");
  10. const locationCtrl = require("./controllers/locationController.js");
  11. const loginCtrl = require("./controllers/loginController.js");
  12. const contactCtrl = require("./controllers/contactController.js");
  13. const registrationCtrl = require("./controllers/registrationController.js");
  14.  
  15. var app = express();
  16.  
  17. /* --------------------------------------------------------------------- */
  18. /* --------------------------- Api Router ------------------------------ */
  19. /* --------------------------------------------------------------------- */
  20.  
  21. var apiRouter = express.Router();
  22.  
  23. // body-parser middleware
  24. apiRouter.use(bodyParser.json());
  25. apiRouter.use(bodyParser.urlencoded({ extended: false }));
  26.  
  27. apiRouter.use(function(req, res, next) {
  28.   res.header("Access-Control-Allow-Origin", "*");
  29.   res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
  30.   res.header(
  31.     "Access-Control-Allow-Headers",
  32.     "Origin, Authorization, X-Access-Token, X-Requested-With, Content-Type, token"
  33.   );
  34.   next();
  35. });
  36.  
  37. // Authentication Router
  38. var authRouter = express.Router();
  39. authRouter.use(authentication.checkToken);
  40.  
  41. //Authencticated routes
  42. var moderationMode = function(req, res, next) {
  43.   req.mode = "MODERATE";
  44.   next();
  45. };
  46. var consultMode = function(req, res, next) {
  47.   req.mode = "CONSULT";
  48.   next();
  49. };
  50.  
  51. authRouter.get("/announces", consultMode, announceCtrl.getAllAnnounces);
  52. authRouter.get("/announces/:id", consultMode, announceCtrl.getAnnounceById);
  53. authRouter.post("/announces", consultMode, announceCtrl.createAnnounce);
  54. authRouter.delete("/announces/:id", consultMode, announceCtrl.deleteAnnounce);
  55. authRouter.put("/announces/:id", consultMode, announceCtrl.updateAnnounce);
  56.  
  57. authRouter.get("/location/regions", locationCtrl.getAllRegions);
  58. authRouter.get(
  59.   "/location/:region/departements",
  60.   locationCtrl.getDepartementsInRegion
  61. );
  62.  
  63. authRouter.get("/contacts/:id", contactCtrl.getContactById);
  64. authRouter.get("/nom", contactCtrl.getNameById);
  65. authRouter.get("/pagecse", contactCtrl.getContenu);
  66. authRouter.post("/updateB", contactCtrl.setBillet);
  67. authRouter.post("/updateV", contactCtrl.setVacances);
  68. authRouter.post("/updateA", contactCtrl.setVente);
  69. authRouter.get(
  70.   "/recherche",
  71.   [
  72.     check("famille").isAlpha(),
  73.     check("activite").isAlpha(),
  74.     check("region").isAlpha(),
  75.     check("dept").isAlpha(),
  76.     check("qtmax").isNumeric(),
  77.     check("qtmin").isNumeric(),
  78.     check("exp").isAfter(new Date())
  79.   ],
  80.   announceCtrl.searchAnnounce
  81. );
  82.  
  83. authRouter.get(
  84.   "/moderation/announces",
  85.   moderationMode,
  86.   announceCtrl.getAllAnnounces
  87. );
  88. authRouter.get(
  89.   "/moderation/announces/:id",
  90.   moderationMode,
  91.   announceCtrl.getAnnounceById
  92. );
  93. authRouter.post(
  94.   "/moderation/announces/",
  95.   moderationMode,
  96.   announceCtrl.updateAnnounce
  97. );
  98. authRouter.get(
  99.   "/moderation/subscriptions",
  100.   moderationMode,
  101.   registrationCtrl.getPendingSubscriptions
  102. );
  103. authRouter.post(
  104.   "/moderation/subscriptions",
  105.   moderationMode,
  106.   registrationCtrl.validateSubscriptions
  107. );
  108. apiRouter.options("*", (req, res, next) => {
  109.   res.status(200).send();
  110. }); //Quand le navigateur t'envoie une requête OPTIONS
  111. //Il faut toujours répondre OK sinon il boude.
  112. apiRouter.get("/login", loginCtrl.handler);
  113. apiRouter.post("/register", registrationCtrl.register);
  114. apiRouter.use(authRouter);
  115.  
  116. //Unhandled routes
  117. apiRouter.all("*", (req, res, next) => {
  118.   res.status(404).json({
  119.     success: false,
  120.     message: "Resource not found"
  121.   });
  122. });
  123.  
  124. /* --------------------------------------------------------------------- */
  125. /* --------------------------------------------------------------------- */
  126. /* --------------------------------------------------------------------- */
  127.  
  128. app.use(express.static(path.join(__dirname, "frontend/build")));
  129.  
  130. app.use("/api", apiRouter);
  131.  
  132. if (process.env.NODE_ENV == "production") {
  133.   app.get("*", (req, res) => {
  134.     res.sendFile(path.join(__dirname + "/frontend/build/index.html"));
  135.   });
  136. }
  137.  
  138. app.listen(serverPort, function() {
  139.   console.log("Server started on port " + serverPort);
  140. });
  141.  
  142. /*
  143.  * 200 OK
  144.  * 201 Created
  145.  * 202 Accepted
  146.  * 204 No Content
  147.  * 400 Bad Request
  148.  * 401 Unauthorized
  149.  * 402 Payment Required
  150.  * 403 Forbidden
  151.  * 404 Not Found
  152.  * 409 Conflict
  153.  * 411 Length Required
  154.  * 500 Internal Server Error
  155.  * 502 Bad Gateway ou Proxy Error
  156.  * 503 Service Unavailable
  157.  * 504 Gateway Time-out
  158.  */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top