I'm following this tutorial and I'm stuck at the end. getting 404's in postman when using POST with this URL http://localhost:5050/api/projects
its been 3 days any one know what I'm doing wrong?
server.js
const express = require("express");
const app = express();
const PORT = 5050;
// Connect to the database
require("./src/server/database/connection");
const bodyParser = require("body-parser");
const cors = require("cors");
var corsOptions = {
origin: "http://localhost:5051"
};
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
// simple route
app.get("/", (req, res) => {
res.json({ message: "Code Wire Server." });
});
require("./src/server/routes/routes")(app);
app.listen(PORT, () => {
console.log(`Server is listening at http://localhost:${PORT}`);
});
connection.js How to connect to the database
const db = require("../model/index.js");
db.mongoose
.connect(db.url, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log("Connected to the database!");
})
.catch(err => {
console.log("Cannot connect to the database!", err);
process.exit();
});
index.js
const dbConfig = require("../config/db.config.js");
const mongoose = require("mongoose");
mongoose.Promise = global.Promise;
const db = {};
db.mongoose = mongoose;
db.url = dbConfig.url;
db.project = require("./projects_db.js")(mongoose);
module.exports = db;
db.config.js
module.exports = {
url: "mongodb://127.0.0.1:27017/code-wire-db"
}
projects_db.js A database schema for my project I'm working on
module.exports = mongoose => {
var schema = mongoose.Schema({
project_title: String,
description: String,
} );
schema.method("toJSON", function() {
// eslint-disable-next-line no-unused-vars
const { __v, _id, ...object } = this.toObject();
object.id = _id;
return object;
});
const projectsDB = mongoose.model("projectsDB", schema);
return projectsDB;
projects_controller.js
const db = require("../model/index");
const project = db.project;
// Create and Save a new project
exports.create = (req, res) => {
// Validate request
if (!req.body.title) {
res.status(400).send({ message: "Content can not be empty!" });
return;
}
// Create a project
const Project = new project({
project_title: req.body.project_title,
description: req.body.description
});
// Save project in the database
Project
.save(Project)
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Project."
});
});
};
routes.js
module.exports = app => {
const projects = require("../controller/projects_controller.js");
var router = require("express").Router();
// Create a new project
router.post("/", projects.create);
app.use('/api/projects', router);
};
CodePudding user response:
I found the problem. [![problem][1]][1]
See that return symbol, I found out 4 days later that means there is 2 lines or more. Meaning postman was sending 2 lines instead of one. With no error message [1]: https://i.stack.imgur.com/VVlha.jpg