1.7.2 • Published 4 years ago

nd5-mongodb-server v1.7.2

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

Project

Dynamic express server with mongodb and websocket.

How to use

  • To start the express server call function StartServer
import { StartServer } from "nd5-mongodb-server";
import * as path from "path";

const dbs = [
  { db: { name: "test", collections: ["cars"] } },
];

const mongodb = "mongodb+srv://<username>:<password>@cluster-ouywc.mongodb.net/test?retryWrites=true&w=majority"

StartServer({
  middleWare,
  socketServer,
  controllersPath: path.join(__dirname, "src", "controllers"),
  mongoDB: {
    url: mongodb
    useNewUrlParser: true,
    useUnifiedTopology: true,
    dbs: dbs
  }
});

function socketServer(io: SocketIO.Server) {
  io.on("connection", client => {
    console.log("Client " + client.id + " connected.");

    io.emit("message", {
      message: "Hello clients, new connection " + client.id
    });

    client.on("message", () => {
      io.emit("message", {
        message: "You have new message from " + client.id
      });
    });
    client.on("disconnect", () => {
      console.log("Good bye " + client.id);
      io.emit("message", {
        message: "Client " + client.id + " disconnected =("
      });
    });
  });
}

function middleWare(req, res, next) {
    next();
}

Creating your route

  • Create src/controllers path.
  • Create file into controllers path. The file name represents route path.
  • Example file cars:
import * as carsCollections from "./../collections/cars";
import express from "express";

const router = express.Router();

router.get("/", async (req, res) => {
  try {
    let cars = await carsCollection.getCars();
    res.json(cars);
  } catch (e) {
    res.status(500).json(e);
  }
});

module.exports = router;

Creating your collection controller

  • Create collections path.
  • Use getColletion function to use your collection:
import { getColletion } from "nd5-mongodb-server/mongo";

export interface ICar {
  name: string;
}

const { collection } = getColletion({
  collection: "cars",
  db: "test"
});

export async function getCars(): Promise<ICar[]> {
  try {
    return await collection.find().toArray();
  } catch (e) {
    throw "Error searching cars. " + e;
  }
}
1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.9

4 years ago

1.6.8

4 years ago

1.6.7

4 years ago

1.6.6

4 years ago

1.6.4

4 years ago

1.6.5

4 years ago

1.6.3

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.9

4 years ago

1.5.8

4 years ago

1.5.7

4 years ago

1.5.6

4 years ago

1.5.5

4 years ago

1.5.4

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.9

4 years ago

1.4.8

4 years ago

1.4.7

4 years ago

1.4.6

4 years ago

1.4.5

4 years ago

1.4.4

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.9

4 years ago

1.3.8

4 years ago

1.3.7

4 years ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.9

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.1.2

4 years ago

1.0.9

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.3

4 years ago

1.0.0

4 years ago