Node
Q & A
Express App

Express

1.What is express js?

Express.js is a popular Node.js web application framework that provides a set of features to build web applications and APIs easily. It provides a thin layer of basic web application features, such as routing, middleware, and support for HTTP requests and responses, while still allowing developers the flexibility to customize and extend the framework as needed. Express.js is highly modular, which means developers can use different modules to add features like authentication, security, and database support to their applications. Additionally, it has a large and active community of contributors, which means it is constantly being updated with new features and bug fixes.

const express = require("express");
const mongoose = require("mongoose");
const app = express();
 
app.get(
  "/",
  (req, res, next) => {
    req.message = "Hello word!!!";
    next();
  },
  (req, res) => {
    const msg = req.message;
    res.send(msg);
  }
);
 
app.listen(8001, () => {
  console.log("app connected port 8001");
});
 
mongoose.connect("mongodb://localhost:27017/test", {});
 
mongoose.connection.on("connected", () => {
  console.log("MongoDB Connected");
});
 
mongoose.connection.on("error", (err) => {
  console.log(err);
});
 
const model = mongoose.model("interview", { name: String });
model.create({ name: "dinesh" });
2.Create two Node.js services that communicate to exchange generic objects. Each service should have three API endpoints, for a total of six. Use a database to save the data.

To create two Node.js services that communicate to exchange generic objects, we can use a combination of Express.js, MongoDB, and Axios. One service will serve as the API provider, while the other service will consume the APIs.

Here are the steps to implement this:

  1. Set up the API provider service using Express.js and MongoDB.
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
 
const app = express();
const port = 3000;
 
app.use(bodyParser.json());
 
mongoose.connect("mongodb://localhost:27017/mydatabase", {
  useNewUrlParser: true,
});
 
const schema = new mongoose.Schema({
  name: String,
  value: mongoose.Schema.Types.Mixed,
});
 
const MyModel = mongoose.model("MyModel", schema);
 
app.post("/api", (req, res) => {
  const { name, value } = req.body;
  const newObject = new MyModel({ name, value });
  newObject
    .save()
    .then(() => {
      res.send("Object saved");
    })
    .catch(() => {
      res.status(500).send("Error saving object");
    });
});
 
app.get("/api", (req, res) => {
  MyModel.find()
    .then((objects) => {
      res.send(objects);
    })
    .catch(() => {
      res.status(500).send("Error retrieving objects");
    });
});
 
app.delete("/api/:id", (req, res) => {
  MyModel.findByIdAndDelete(req.params.id)
    .then(() => {
      res.send("Object deleted");
    })
    .catch(() => {
      res.status(500).send("Error deleting object");
    });
});
 
app.listen(port, () => {
  console.log(`API provider listening at http://localhost:${port}`);
});
  1. Set up the API consumer service using Axios.
const axios = require("axios");
 
const getObject = async () => {
  const response = await axios.get("http://localhost:3000/api");
  console.log(response.data);
};
 
const postObject = async (name, value) => {
  const response = await axios.post("http://localhost:3000/api", {
    name,
    value,
  });
  console.log(response.data);
};
 
const deleteObject = async (id) => {
  const response = await axios.delete(`http://localhost:3000/api/${id}`);
  console.log(response.data);
};
 
getObject();
postObject("object1", { foo: "bar" });
deleteObject("object-id");
  1. Start both services and test the endpoints.

The API provider service will listen on port 3000, while the API consumer service will use Axios to make requests to the API provider service. The endpoints are /api for POST and GET, and /api/:id for DELETE. The objects are stored in a MongoDB database using Mongoose.

Note that this is just a simple example to demonstrate the concept of two services communicating with each other to exchange generic objects. In practice, there would be more considerations to take into account, such as security and scalability.