Search code examples
node.jsexpressserverpostmanbackend

Express cannot post


I am trying to make a contact manager and I cannot post anything to create a contact.

Below are my files and their content

I think i might be making silly mistake. But I'm unable to find where I m going wrong.

Server.js

const dotenv = require("dotenv");
dotenv.config();

const express = require("express");
const app = express();

app.use(express.json());

const { mongoDB, server } = require("./utils/functions");

//Database
mongoDB();

//Routes Imports
const contactRoute = require("./Routes/contactRoutes");

//Routes
app.use("/api/contact", contactRoute);

server();

functions.js

const express = require("express");
const mongoose = require("mongoose");

const app = express();

exports.mongoDB = () => {
  const mongo_url = process.env.MONGO_URL;
  mongoose.connect(mongo_url).then(() => {
    console.log("MongoDB database connected");
  });
};

exports.server = () => {
  const port = process.env.PORT;
  app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
  });
};

contactRoutes.js

const express = require("express");
const {
  createContact,
  getAllContact,
  getContact,
} = require("../Controller/contactController");

const router = express.Router();

router.post("/new", createContact);

router.get("/allContacts", getAllContact);

router.get("contact", getContact);

module.exports = router;

contactController.js

const contactModel = require("../model/contactModel");

exports.createContact = async (req, res) => {
  console.log(req.body);
  //create contact
  const newContact = await contactModel.create(req.body);

  //log contact
  res.status(200).json({ success: true, newContact });
};
exports.getAllContact = () => {};
exports.getContact = () => {};

contactModel.js

const { default: mongoose } = require("mongoose");

const contactSchema = new mongoose.Schema({
  firstName: {
    type: String,
  },
  lastName: {
    type: String,
  },
  phoneNumber: {
    type: Number,
  },
});

module.exports = mongoose.model("contacts", contactSchema);

Postman

enter image description here

I tried all I know but I was unable to solve it.

Also "if you can" tell me something to handle errors


Solution

  • The problem is that in the functions.js file, a new Express app instance is created, which has no routes defined, it just starts a server, and gives the error on that route, and any other:

    // this is a new instance...
    const app = express();
    
    exports.server = () => {
      const port = process.env.PORT;
      app.listen(port, () => {
        console.log(`Server is running on port ${port}`);
      });
    };
    

    To fix this, pass the same app instance instead to the function in server.js.

    Try this:

    Server.js

    // pass this app instance
    server(app);
    

    functions.js

    // takes app instance as an argument
    exports.server = (app) => {
      const port = process.env.PORT;
      app.listen(port, () => {
        console.log(`Server is running on port ${port}`);
      });
    };