This is my server.js:
const express= require('express');
const app = express();
// const mongoose= require('mongoose');
// load config from env file
require("dotenv").config();
const PORT = process.env.PORT || 4000;
// middleware to parse json request body
app.use(express.json());
// import routes for Todo API
const todoRoutes = require('./routes/todo');
// mount the todo API routes
app.use("api/v1", todoRoutes);
// now the server
app.listen(3000,()=>{
console.log(`server started on port 3000`)
})
// connect to the database
const dbConnect= require('./config/database');
dbConnect();
// default route
app.get("/",((res)=>{
res.send(`<h1> This is the home page</h1>`)
}))
And this is my database.js --
const mongoose= require('mongoose');
require("dotenv").config();
const dbConnect=()=>{
mongoose.connect(process.env.DATABASE_URL,{
useNewUrlParser:true,
useUnifiedTopology:true
})
.then(()=>console.log("connection ho gya "))
.catch((error)=>{
console.log("connction nhi hua ")
console.error(error.message)
// process.exit(1);
})
}
module.exports = dbConnect;
And this is my Todo.js --
const mongoose = require("mongoose");
// Here is the schema --> todoSchema
const todoSchema = new mongoose.Schema({
title: {
type: String, // type of data in the title
required: true,
maxLength: 50,
},
description: {
type: String,
required: true,
maxLength: 50, // maximum length of the description
},
createdAt: {
type: Date,
required: true,
default: Date.now(), //default value
},
updatedAt: {
type: Date,
required: true,
default: Date.now(),
},
});
// this statement is required for exporting the model
// and this is exporting the todoSchema but with the name "Todo"
module.exports = mongoose.model("Todo", todoSchema);
And this is my createTodo.js --
const Todo = require("../models/Todo");
// defining route handler
exports.createTodo = async (req, res) => {
try {
// extract title and description from request body
const { title, description } = req.body;
// create a new Todo Object and insert in Database
const response = await Todo.create({ title, description });
// send a json response with a success flag
res.status(200).json({
success: true,
data: response,
message: "Entry created successfully",
});
} catch (err) {
console.error(err);
console.log(err);
res.status(500).json({
success: false,
data: "internal server error",
message: err.message,
});
}
};
And this is my todo.js --
const express= require('express');
const router=express.Router();
// importing controllers
const { createTodo } = require('../controllers/createTodo');
// defining routes
router.post('/createTodo',createTodo );
// exporting
module.exports = router;
Now whenever I am sending data from Postman API app to my database, I am getting this --
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /api/v1/createTodo</pre>
</body>
</html>
and data is not getting inserted in my database
I am unable to get why I am unable to get success flag in response and send data to database
The following statement app.use("api/v1", todoRoutes);
in server.js
is missing a /
in the "api/v1"
string.
It should be "/api/v1"
.
You can learn more about routing in the Express documentation.