Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Main IMPORTS
- const express = require("express");
- const bodyParser = require("body-parser");
- // IMPORTS END
- const app = express();
- // Server Configs Settings
- const PORT = process.env.PORT || 8000;
- app.use(bodyParser.urlencoded({
- extended: false
- }));
- // Configure BodyParser to read json data
- app.use(bodyParser.json());
- // End
- // Global Vars Configure
- // Office Data Structure
- let officeData = {
- "error":"none",
- "officeName": "",
- "officeAddress": "",
- "audianceRT": "",
- "estimatedTime": "",
- "pastData": [],
- "counterSchedule": ["", "", ""],
- "lat": "",
- "long": ""
- };
- // Offices Array will hold every office location
- let offices = [];
- //End
- // Dummy Offices Data With ID
- office = officeData;
- office.error="success";
- office.officeName = "SBI Bank";
- office.officeAddress = "Gahra Chowk Jabalpur, Madhya Pradesh, India";
- office.audianceRT = "10";
- office.estimatedTime = "20 Mins";
- office.counterSchedule = ["10:00 AM", "2:00 PM", "5:30 PM"];
- office.pastData = [15,8,9,12,9,15];
- office.lat = "23.165376";
- office.long = "79.898441";
- offices[121] = office;
- // Allow Cross Domain Request Headers
- app.use((req,res,next)=>{
- res.header("Access-Control-Allow-Origin", "*");
- res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
- next();
- });
- // Server Endpoints
- app.get("/v1/officeData/:id", (req, res) => {
- //Getting Office ID form the URL
- const officeID = req.params.id;
- try {
- if (offices[req.params.id] == undefined) {
- throw "Sad";
- }
- res.send(JSON.stringify(offices[req.params.id]));
- console.log("Querry Sent! Success");
- } catch (error) {
- res.send(JSON.stringify({
- "error": "Looks Like Something Messed UP!"
- }));
- console.log("Something Messed Up!");
- }
- });
- //End
- app.get("/",(req,res)=>{
- res.send("Looks Like You Forgot Something");
- });
- // Data Update Endpoint
- app.get("/v1/officeDataUpdateRT/:id/:people",(req,res)=>{
- const officeID = req.param.id;
- const updatePeole = req.param.people;
- // To Check if the id in array exist
- try{
- // Get office data object
- let toUpdateOffice = offices[officeId];
- toUpdateOffice.audianceRT = updatePeople;
- console.log("Data Updated for Camera ID : "+officeID+"\nUpdated Audiance : "+updatePeole);
- }
- catch(error){
- console.log("Some Thing Messed Up To Update For Camera ID : "+officeID);
- console.log("With Error :\n"+error);
- }
- });
- app.post('/v1/setCamera',(req,res)=>{
- let CameraSetData = req.body;
- let setOffice = officeData;
- // Setting Office Data From Camera Data
- setOffice.officeName = CameraSetData.officeName;
- setOffice.officeAddress = CameraSetData.officeAddress;
- setOffice.audianceRT = CameraSetData.audianceRT == undefined ? 0 : CameraSetData.audianceRT;
- console.log(setOffice);
- });
- //For Message Server
- app.get("/v1/makeMessage/:id",(req,res)=>{
- const officeID = req.params.id;
- try {
- if (offices[req.params.id] == undefined) {
- throw "Sad";
- }
- let data = offices[req.params.id];
- res.send("Office Name : "+data.officeName+"\nCurrent Audiance : "+data.audianceRT+"\nEstimated Time : "+data.estimatedTime);
- console.log("Querry Sent! Success");
- } catch (error) {
- res.send(JSON.stringify({
- "error": "Looks Like Something Messed UP!"
- }));
- console.log("Something Messed Up!");
- }
- });
- //Server Start
- app.listen(PORT, () => {
- console.log("Server Started On : http://127.0.0.1:" + PORT);
- });
Add Comment
Please, Sign In to add comment