nodejsnotes

Topic 009: Basic features in Node.js

Topic 009: Basic features in Node.js

1. Infinite Scroll:

// Server-side code (assuming you have an endpoint to fetch data)
app.get("/data", (req, res) => {
  const page = req.query.page || 1;
  const pageSize = 10;
  const start = (page - 1) * pageSize;
  const end = start + pageSize;

  // Fetch data from the database
  YourModel.find()
    .skip(start)
    .limit(pageSize)
    .exec((err, data) => {
      if (err) {
        return res.status(500).json({ error: "Internal server error" });
      }
      res.json(data);
    });
});

2. Filtering:

app.get("/data", (req, res) => {
  const { category, keyword } = req.query;

  let query = {};

  if (category) {
    query.category = category;
  }

  if (keyword) {
    query.$or = [
      { name: { $regex: keyword, $options: "i" } },
      { description: { $regex: keyword, $options: "i" } },
    ];
  }

  YourModel.find(query, (err, data) => {
    if (err) {
      return res.status(500).json({ error: "Internal server error" });
    }
    res.json(data);
  });
});

3. Sorting:

app.get("/data", (req, res) => {
  const { sortBy } = req.query;

  YourModel.find()
    .sort(sortBy)
    .exec((err, data) => {
      if (err) {
        return res.status(500).json({ error: "Internal server error" });
      }
      res.json(data);
    });
});
app.get("/data", (req, res) => {
  const { searchQuery } = req.query;

  YourModel.find({ $text: { $search: searchQuery } }, (err, data) => {
    if (err) {
      return res.status(500).json({ error: "Internal server error" });
    }
    res.json(data);
  });
});

5. Data Visualization:

// Assuming you have an endpoint to fetch data for visualization
app.get("/visualization-data", (req, res) => {
  // Fetch data from the database
  YourModel.find({}, (err, data) => {
    if (err) {
      return res.status(500).json({ error: "Internal server error" });
    }
    res.json(data);
  });
});

6. Lazy Loading:

// Assuming you have an endpoint to fetch data for lazy loading
app.get("/lazy-load-data", (req, res) => {
  const { startIndex, count } = req.query;

  YourModel.find()
    .skip(startIndex)
    .limit(count)
    .exec((err, data) => {
      if (err) {
        return res.status(500).json({ error: "Internal server error" });
      }
      res.json(data);
    });
});

7. Caching:

Caching is often implemented using external libraries like Redis or Memcached. Here’s a simplified example using in-memory caching:

const cache = {};

app.get("/cached-data", (req, res) => {
  const cacheKey = req.query.cacheKey;

  if (cache[cacheKey]) {
    return res.json(cache[cacheKey]);
  }

  YourModel.find({}, (err, data) => {
    if (err) {
      return res.status(500).json({ error: "Internal server error" });
    }
    cache[cacheKey] = data;
    res.json(data);
  });
});

8. Real-time Updates:

const http = require("http");
const server = http.createServer(app);
const io = require("socket.io")(server);

io.on("connection", (socket) => {
  console.log("A user connected");

  // Listen for data updates
  YourModel.watch().on("change", (data) => {
    socket.emit("dataUpdate", data);
  });

  socket.on("disconnect", () => {
    console.log("User disconnected");
  });
});

server.listen(3000, () => {
  console.log("Server is running on port 3000");
});

9. Pagination with Cursor-based Navigation:

app.get("/data", (req, res) => {
  const { cursor } = req.query;
  const limit = 10;

  let query = {};

  if (cursor) {
    query._id = { $gt: cursor };
  }

  YourModel.find(query)
    .limit(limit)
    .exec((err, data) => {
      if (err) {
        return res.status(500).json({ error: "Internal server error" });
      }
      res.json(data);
    });
});

10. Batch Processing:

// Assuming you have a batch processing task to perform
app.post("/batch-process", (req, res) => {
  const { batchSize } = req.body;

  YourModel.find({ processed: false })
    .limit(batchSize)
    .exec((err, data) => {
      if (err) {
        return res.status(500).json({ error: "Internal server error" });
      }

      // Perform batch processing logic here

      res.json({ message: "Batch processing completed" });
    });
});

These are simplified examples to demonstrate the implementation of each functionality. Depending on your specific use case and requirements, you may need to adapt and expand upon these examples.