This is my main file index.js
type here const express = require("express"); const app = express(); require("dotenv").config(); const port = process.env.port || 4000; app.use(express.json()); const createBlog = require("./routes/blogRoutes"); app.use("/santi/api/v1",createBlog); app.listen(port, () => { console.log("App run in 3000 port"); }) const dbConnect = require("./config/database"); dbConnect(); app.get("/", (req,res) => { res.send(`<h1>this is homepage</h1>`); })
This is my route
const express = require("express"); const router = express.Router(); const {createBlog} = require("../controlers/createBlog"); router.post("/createBlog",createBlog); module.exports = router;
This is my controller file
const createPost = require("../models/post"); exports.post = async (req,res) => { try { const {title,description} = req.body; const response = await createPost.create({title,description}); res.status(200).json({ status:true, message:"Insert seccessfully", data:response }) } catch(error) { console.error(err); res.status(500).json({ status:false, message:error.message, data:"Server issue" }) } }
This is my model file
const mongoose = require("mongoose"); const blogPostSchema = new mongoose.Schema( { title:{ type:String, required:true, maxlength:50 }, description:{ type:String, required:true, maxlength:50 } } ) module.exports = mongoose.model("blogPost",blogPostSchema);
I installed nodemon and mongoose but still getting error in terminal after writing "npm run dev" command
[email protected] Developer Nodemon index.js
[nodemon] 2.0.22
[nodemon] Restart at any time, enter rs
[nodemon] Monitoring path: .
[nodemon] View extensions: js, mjs, json
[nodemon] Start node index.js
C:\Users\SWETTA\OneDrive\Desktop\blog_backend\node_modules\express\lib\router\route.js:211
throw newError(message);
^
Error: Route.post() expected a callback function but got one [object undefined] on the route. [As post] (C:\Users\SWETTA\OneDrive\Desktop\blog_backend\node_modules\express\lib\router\route.js:211:15) in prototype. [As post] (C:\Users\SWETTA\OneDrive\Desktop\blog_backend\node_modules\express\lib\router\index.js:521:19) in object. (C:\Users\SWETTA\OneDrive\Desktop\blog_backend\routes\blogRoutes.js:6:8) in Module._compile (node:internal/modules/cjs/loader:1254:14) in Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Module.load(node:internal/modules/cjs/loader:1117:32) in Module._load (node:internal/modules/cjs/loader:958:12) in Module.require (node:internal/modules/cjs/loader:1141:19) when needed (node:internal/modules/cjs/helpers:110:18) in object. (C:\Users\SWETTA\OneDrive\Desktop\blog_backend\index.js:9:20)
What is the solution?
The problem is in your controller file, you are exporting a function called post and importing it as createBlog into your routes file. To correct your code you should update your controller and rename the function to createBlog :
I found a small bug in your router code.
You export the function in the controller as
post
and import it in the router ascreateBlog
. And your controller doesn't export anything namedcreateBlog
, hence the problem.Just change the router code to: