Advertisement
Guest User

Untitled

a guest
Mar 7th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. const bodyParser = require("body-parser");
  2. const express = require("express");
  3. const fs = require("fs");
  4. const mongodb = require("mongodb")
  5. const spdy = require("spdy");
  6.  
  7. const MongoClient = mongodb.MongoClient;
  8. const Server = mongodb.Server;
  9.  
  10. const app = express();
  11.  
  12. let hostname = "localhost"
  13. let port = 207017
  14. let database = "driverDB"
  15. let drivers_collection = "drivers"
  16.  
  17. app.use(bodyParser.json());
  18.  
  19.  
  20.  
  21. /*
  22. Function that a driver will use to REGISTER with the enterprise.
  23. HTTP Method POST
  24. Params: Name, Username, Password (in header)
  25. Response:
  26. 201 OK if USER SUCCESSFULLY CREATED
  27. then ADD { username, name } entry in // DB
  28. 403 FORBIDDEN if Username already taken
  29. 500 INTERNAL SERVER ERROR
  30. */
  31. app.post("/register", async (reqt, resp) => {
  32. try{
  33. const username = reqt.body.username;
  34. const password = reqt.body.password;
  35. const hashed_password = await bcrypt.hashSync(password, 10);
  36. const name = reqt.body.name;
  37. const svr = new Sever(hostname, port);
  38. const con = await MongoClient.connect(svr);
  39. const col = con.db(database).collection(drivers_collection)
  40.  
  41. //SEND USERNAME AND PASSWORD TO AUTHENTICATION PROVIDER
  42. const response =
  43.  
  44. //IF AUTH RESPONSE 201 THEN RETURN 201 AND ADD USERNAME AND NAME TO DB
  45. if (response == 201) {
  46.  
  47. const res = await col.updateOne( { username : username },
  48. {$set : { username : response.body.password,
  49. name : name } },
  50. {upsert : true }
  51. );
  52. con.close();
  53. resp.status(201).end();
  54.  
  55. }
  56.  
  57. //IF AUTH RESPONSE 403 THEN RETURN 403 (USERNAME WAS TAKEN)
  58.  
  59. else if (response == 403) {
  60. con.close(); //DO I NEED THIS?
  61. resp.status(403).end();
  62. }
  63.  
  64.  
  65. //TO LOGIN NEXT??
  66. next();
  67. } catch (exn) {
  68. console.log(exn);
  69. resp.status(500).end();
  70. }
  71. })
  72.  
  73.  
  74.  
  75.  
  76. /*
  77. Function that a driver will use to join the roster
  78. HTTP Method PUT
  79. Params: username, lowest rate?
  80. Response:
  81. 200 OK if driver successfully joins the roster
  82. database store latest rate and maybe return lowest rate
  83. 403 FORBIDDEN if the driver cannot join for some reason
  84. 500 INTERNAL SERVER ERROR
  85. */
  86. app.put("/join-roster", async (reqt, resp) => {
  87. try{
  88. const username = reqt.body.username;
  89. const rate = reqt.body.rate;
  90. const svr = new Sever(hostname, port);
  91. const con = await MongoClient.connect(svr);
  92. const col = con.db(database).collection(drivers_collection)
  93.  
  94. //IF ROSTER RESPONSE 200 THEN RETURN 200, MAYBE STORE AND RETURN RATE
  95. if (response == 200){
  96.  
  97. const res = await col.updateOne( { username : username },
  98. {$set : { rate : rate } },
  99. {upsert : true }
  100. );
  101.  
  102. con.close();
  103. resp.send({ rate : rate });
  104. resp.status(200).end();
  105. }
  106.  
  107. //IF ROSTER 403 THEN RETURN 403 (FORBIDDEN TO JOIN FOR SOME REASON)
  108. if (response == 403){
  109. resp.status(403).end();
  110. }
  111.  
  112. } catch (exn) {
  113. resp.status(500).end();
  114. }
  115. })
  116.  
  117.  
  118.  
  119.  
  120. /*
  121. Function that a driver will use to update the roster with thier new lowest rate
  122. HTTP method PUT
  123. Params: username, lowest rate
  124. Response:
  125. 200 OK if lowest rate successfully updated
  126. store latest rate in DB
  127. 404 NOR FOUND if driver cannot be found or is not in roster
  128. 403 FORBIDDEN if driver rate cannot be updated for some reason (on a current job)
  129. 501 INTERNAL SERVER ERROR
  130. */
  131. app.put("/update-rate", async (reqt, resp) => {
  132. try{
  133. const username = reqt.body.username;
  134. const rate = reqt.body.rate;
  135. const svr = new Sever(hostname, port);
  136. const con = await MongoClient.connect(svr);
  137. const col = con.db(database).collection(drivers_collection)
  138.  
  139.  
  140.  
  141. //IF ROSTER RETURNS 200 THEN RETURN 200 and store latest rate in DB
  142. if (response == 200){
  143.  
  144. const res = await col.updateOne( { username : username },
  145. {$set : { rate : response.body.rate } }.
  146. {upsert : true }
  147. );
  148. con.close();
  149. resp.status(200).end();
  150. }
  151.  
  152. //IF ROSTER RETURNS 404 THEN RETRUN 404
  153. if (response == 404){
  154. resp.status(404).end();
  155. }
  156.  
  157. //IF ROSTER RETURNS 403 THEN RETURN 403
  158. if (response == 403) {
  159. resp.status(403).end();
  160. }
  161.  
  162. } catch (exn) {
  163. resp.status(500).end();
  164. }
  165. })
  166.  
  167.  
  168.  
  169.  
  170. /*
  171. Function that a driver will use to leave the roster
  172. HTTP Method PUT
  173. Params: username
  174. Response:
  175. 200 OK if driver successfully leaves the roster
  176. 403 FORBIDDEN if the driver cannot join for some reason
  177. 500 INTERNAL SERVER ERROR
  178. */
  179. app.put("/leave-roster", async (reqt, resp) => {
  180. try{
  181. const username = reqt.body.username;
  182. const svr = new Sever(hostname, port);
  183. const con = await MongoClient.connect(svr);
  184. const col = con.db(database).collection(drivers_collection)
  185.  
  186. //IF ROSTER RESPONSE 200 THEN RETURN 200
  187. if (response == 200){
  188. resp.status(200).end();
  189. }
  190.  
  191. } catch (exn) {
  192. resp.status(500).end();
  193. }
  194. })
  195.  
  196.  
  197.  
  198.  
  199. //RUNNING THE SCRIPT
  200.  
  201. //CREATE SERVER USING THE GENERATED CERTIFICATES TO USE HTTPS CHANNEL
  202. const server = spdy.createServer( {
  203. key : fs.readFileSync("key.pem"),
  204. cert : fs.readFileSync("cert.pem")
  205. }, app );
  206.  
  207.  
  208. server.listen( 8443, () => {
  209. console.log("listen on port 8443...\n");
  210. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement