I have gone through the other similar posts but not sure or experienced enough to relate the resolution suggestions on my project.
I have created a simple app with two main routes as part of my learning through a udemy course, however, i cannot get around this issue of Error: Route.get() requires a callback function but got a [object String]. I know that that main aim is to export the router object containing all the nested routes for a root route but the fact that the error is asking for a callback doesn’t make sense to me. Also the source of the error is the route.js file in the main node_modules library, node_modulesexpresslibrouterroute.js:211:15.
Tried a new test controller, same issue when running that file even without importing any of the route files. Deleted the route.js file from the library, new error shows up, Error: Cannot find module ‘./route’. Officially stuck.
Insights and suggestions would be absolutely appreciated .
I have added the route, controller and package.json files below for reference.
controller (app.js)
const express = require('express');
const ejs = require('ejs');
const Joi = require('joi');
const methodOverride = require('method-override');
const mongoose = require('mongoose');
const flash = require('connect-flash');
const session = require('express-session');
const path = require('path');
const ejsMate = require('ejs-mate');
const app = express();
mongoose.connect('mongodb://127.0.0.1:27017/yelp-camp-new')
.then(
console.log('Connected to YelpCamp New db')
)
.catch(e => {
console.log('error');
console.log(e);
}
);
app.get('view engine', 'ejs')
app.engine('ejs', ejsMate);
app.set('views', path.join(__dirname, '/views'));
app.use(express.json()) // parse json api data
app.use(express.urlencoded(({ extended: true }))); // parse form data encoded in body
app.use(methodOverride('_method')); // method over-ride for post requests
//Session and flash
const sessionConfig = {
secret: 'Thi$i$m7secret',
resave: false,
saveUninitialized: false,
cookie:{
httpOnly: true, // extra layer of security
_expires: Date.now() + 1000 *60*60*24*7,
maxAge: 1000*60*60*24*7
}
};
app.use(session(sessionConfig));
app.use(flash()); // flash middleware
// Serving Static files from public dir, like js and css files.
app.use(express.static(path.join(__dirname, '/public')));
// importing the routes
const campRoute = require('./routes/campRoutes');
app.use('/campgrounds', campRoute);
// =========================
// error middleware
app.use((err, req, res, next) => {
const {message = 'Something went wrong.', status = 500} = err;
res.status(status);
res.render('error', {err, status})
next(err);
})
// middleware for all the other routes not defined in this app
app.use('*', (req, res) =>{
res.send('This page does not exist')
})
app.listen(3000, ()=>{
console.log('Listening on port 3000')
})
//==================================================================================================
route file (campRoutes.js)
const express = require('express');
const router = express.Router();
// pott.get('/', async (req, res)=>{
// const camps = await Campground.find({});
// res.render('showAllCamps', {camps});
// });
router.get('/new', (req, res)=>{
res.render('new');
});
router.post('/', async(req, res)=>{
const {campground} = req.body;
console.log(campground);
})
module.exports = router;
//==================================================================================================
package.json file
{
"name": "yelpcamp",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"connect-flash": "^0.1.1",
"ejs": "^3.1.9",
"ejs-mate": "^4.0.0",
"express": "^4.18.2",
"express-session": "^1.17.3",
"joi": "^17.11.0",
"method-override": "^3.0.0",
"mongoose": "^8.0.1"
}
}