Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const bodyParser = require("body-parser");
- const express = require("express");
- const fs = require("fs");
- const mongodb = require("mongodb")
- const spdy = require("spdy");
- const MongoClient = mongodb.MongoClient;
- const Server = mongodb.Server;
- const app = express();
- let hostname = "localhost"
- let port = 207017
- let database = "driverDB"
- let drivers_collection = "drivers"
- app.use(bodyParser.json());
- /*
- Function that a driver will use to REGISTER with the enterprise.
- HTTP Method POST
- Params: Name, Username, Password (in header)
- Response:
- 201 OK if USER SUCCESSFULLY CREATED
- then ADD { username, name } entry in // DB
- 403 FORBIDDEN if Username already taken
- 500 INTERNAL SERVER ERROR
- */
- app.post("/register", async (reqt, resp) => {
- try{
- const username = reqt.body.username;
- const password = reqt.body.password;
- const hashed_password = await bcrypt.hashSync(password, 10);
- const name = reqt.body.name;
- const svr = new Sever(hostname, port);
- const con = await MongoClient.connect(svr);
- const col = con.db(database).collection(drivers_collection)
- //SEND USERNAME AND PASSWORD TO AUTHENTICATION PROVIDER
- const response =
- //IF AUTH RESPONSE 201 THEN RETURN 201 AND ADD USERNAME AND NAME TO DB
- if (response == 201) {
- const res = await col.updateOne( { username : username },
- {$set : { username : response.body.password,
- name : name } },
- {upsert : true }
- );
- con.close();
- resp.status(201).end();
- }
- //IF AUTH RESPONSE 403 THEN RETURN 403 (USERNAME WAS TAKEN)
- else if (response == 403) {
- con.close(); //DO I NEED THIS?
- resp.status(403).end();
- }
- //TO LOGIN NEXT??
- next();
- } catch (exn) {
- console.log(exn);
- resp.status(500).end();
- }
- })
- /*
- Function that a driver will use to join the roster
- HTTP Method PUT
- Params: username, lowest rate?
- Response:
- 200 OK if driver successfully joins the roster
- database store latest rate and maybe return lowest rate
- 403 FORBIDDEN if the driver cannot join for some reason
- 500 INTERNAL SERVER ERROR
- */
- app.put("/join-roster", async (reqt, resp) => {
- try{
- const username = reqt.body.username;
- const rate = reqt.body.rate;
- const svr = new Sever(hostname, port);
- const con = await MongoClient.connect(svr);
- const col = con.db(database).collection(drivers_collection)
- //IF ROSTER RESPONSE 200 THEN RETURN 200, MAYBE STORE AND RETURN RATE
- if (response == 200){
- const res = await col.updateOne( { username : username },
- {$set : { rate : rate } },
- {upsert : true }
- );
- con.close();
- resp.send({ rate : rate });
- resp.status(200).end();
- }
- //IF ROSTER 403 THEN RETURN 403 (FORBIDDEN TO JOIN FOR SOME REASON)
- if (response == 403){
- resp.status(403).end();
- }
- } catch (exn) {
- resp.status(500).end();
- }
- })
- /*
- Function that a driver will use to update the roster with thier new lowest rate
- HTTP method PUT
- Params: username, lowest rate
- Response:
- 200 OK if lowest rate successfully updated
- store latest rate in DB
- 404 NOR FOUND if driver cannot be found or is not in roster
- 403 FORBIDDEN if driver rate cannot be updated for some reason (on a current job)
- 501 INTERNAL SERVER ERROR
- */
- app.put("/update-rate", async (reqt, resp) => {
- try{
- const username = reqt.body.username;
- const rate = reqt.body.rate;
- const svr = new Sever(hostname, port);
- const con = await MongoClient.connect(svr);
- const col = con.db(database).collection(drivers_collection)
- //IF ROSTER RETURNS 200 THEN RETURN 200 and store latest rate in DB
- if (response == 200){
- const res = await col.updateOne( { username : username },
- {$set : { rate : response.body.rate } }.
- {upsert : true }
- );
- con.close();
- resp.status(200).end();
- }
- //IF ROSTER RETURNS 404 THEN RETRUN 404
- if (response == 404){
- resp.status(404).end();
- }
- //IF ROSTER RETURNS 403 THEN RETURN 403
- if (response == 403) {
- resp.status(403).end();
- }
- } catch (exn) {
- resp.status(500).end();
- }
- })
- /*
- Function that a driver will use to leave the roster
- HTTP Method PUT
- Params: username
- Response:
- 200 OK if driver successfully leaves the roster
- 403 FORBIDDEN if the driver cannot join for some reason
- 500 INTERNAL SERVER ERROR
- */
- app.put("/leave-roster", async (reqt, resp) => {
- try{
- const username = reqt.body.username;
- const svr = new Sever(hostname, port);
- const con = await MongoClient.connect(svr);
- const col = con.db(database).collection(drivers_collection)
- //IF ROSTER RESPONSE 200 THEN RETURN 200
- if (response == 200){
- resp.status(200).end();
- }
- } catch (exn) {
- resp.status(500).end();
- }
- })
- //RUNNING THE SCRIPT
- //CREATE SERVER USING THE GENERATED CERTIFICATES TO USE HTTPS CHANNEL
- const server = spdy.createServer( {
- key : fs.readFileSync("key.pem"),
- cert : fs.readFileSync("cert.pem")
- }, app );
- server.listen( 8443, () => {
- console.log("listen on port 8443...\n");
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement