Advertisement
KoctrX

Untitled

Mar 19th, 2021
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require("express");
  2. const cors = require("cors");
  3. const mongoose = require("mongoose");
  4.  
  5. //Allows me to set up enviroment veriables on the .env file
  6. require("dotenv").config();
  7.  
  8. /* Creates Express server */
  9.  
  10. const app = express();
  11. const port = process.env.PORT || 5000;
  12.  
  13. /*
  14.  * Cors Middleware
  15.  * Parses all incoming and outgoing json
  16.  */
  17. app.use(cors());
  18. app.use(express.json());
  19.  
  20. /*  */
  21.  
  22. const uri = process.env.ATLAS_URI;
  23. mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true });
  24.  
  25. const connection = mongoose.connection;
  26. connection.once("open", () => {
  27.     console.log("MongoDB database connection is established successfully");
  28. });
  29.  
  30. const todoRouter = require("./routes/todos");
  31. const userRouter = require("./routes/users");
  32.  
  33. app.use("/todos", todoRouter);
  34. app.use("/users", userRouter);
  35.  
  36. app.use((err, req, res, next) => {
  37.     res.status(500).send('Server error');
  38. });
  39.  
  40. app.listen(port, () => {
  41.     console.log(`Server is running on port: ${port}`);
  42. });
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement