Skip to content

Instantly share code, notes, and snippets.

@Vatsalya-singhi
Created February 26, 2024 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vatsalya-singhi/029f1b5aeec440395e00977f724cb99b to your computer and use it in GitHub Desktop.
Save Vatsalya-singhi/029f1b5aeec440395e00977f724cb99b to your computer and use it in GitHub Desktop.
// security
// security
app.use(helmet());
app.disable('x-powered-by');
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes).
message: 'Too many requests from this IP, please try again later.',
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
// store: ... , // Use an external store for consistency across multiple server instances.
})
app.use(limiter);
// security
app.use(express.json());
app.use(cors({
// origin: allowedOrigins,
}));
// router paths
app.use("/iot_dumps", iot_dumps_router);
app.use("/temperature", temp_router);
app.use("/humidity", humidity_router);
app.use("/proximity", proximity_router);
app.use("/luminosity", luminosity_router);
app.use("/ldr", ldr_router);
app.use("/led_status", led_status_router);
// router paths
// sample api calls to mongodb database
// GET /
// GET /?limit=100
router.get("/", async (req, res) => {
const limit = req?.query?.limit ? Number(req.query.limit) : 100;
let results = await db.collection(collection)
.find({})
.limit(limit)
.toArray();
res.header("Content-Type", 'application/json');
res.send(JSON.stringify(results, null, 4)).status(200);
});
// GET /device_name_list
router.get("/device_name_list", async (req, res) => {
let results = await db.collection(collection).aggregate([
{ $group: { _id: null, list_of_device_name: { $addToSet: "$device_name" } } },
{ $project: { _id: 0 } }
])
.toArray();
res.header("Content-Type", 'application/json');
res.send(JSON.stringify(results, null, 4)).status(200);
})
// GET /place_id_list
router.get("/place_id_list", async (req, res) => {
let results = await db.collection(collection).aggregate([
{ $group: { _id: null, list_of_place_id: { $addToSet: "$place_id" } } },
{ $project: { _id: 0 } },
])
.toArray();
res.header("Content-Type", 'application/json');
res.send(JSON.stringify(results, null, 4)).status(200);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment