Reputation: 61
When I run node server.js
in the command line I get this error:
C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms>node server.js
C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\node_modules\express\lib\router\index.js:458
throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
^
TypeError: Router.use() requires a middleware function but got a Object
at Function.use (C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\node_modules\express\lib\router\index.js:458:13)
at Function.<anonymous> (C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\node_modules\express\lib\application.js:220:21)
at Array.forEach (<anonymous>)
at Function.use (C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\node_modules\express\lib\application.js:217:7)
at Object.<anonymous> (C:\Users\Shaelyn\WebstormProjects\CIT366Projects\cms\server.js:34:5)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
I think part of it might be that the mongoose.connect
is an unresolved function. Does anyone know how to fix this error?
This is my code:
// Get dependencies
var express = require('express');
var path = require('path');
var http = require('http');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var mongoose = require('mongoose');
// import the routing file to handle the default (index) route
var index = require('./server/routes/app');
const messageRoutes = require('./server/routes/messages');
const contactRoutes = require('./server/routes/contacts');
const documentRoutes = require('./server/routes/documents');
// establish a connection to the mongo database
mongoose.connect('mongodb://localhost:27017/cms');
var app = express(); // create an instance of express
// Tell express to use the following parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(logger('dev')); // Tell express to use the Morgan logger
// Tell express to use the specified director as the
// root directory for your web site
app.use(express.static(path.join(__dirname, 'dist')));
app.use('/', index);
app.use('/messages', messageRoutes);
app.use('/contacts', contactRoutes);
app.use('/documents', documentRoutes);
// Define the port address and tell express to use this port
const port = process.env.PORT || '3000';
app.set('port', port);
// Create HTTP server.
const server = http.createServer(app);
// Tell the server to start listening on the provided port
server.listen(port, function() {console.log("API running on localhost: " +
port)});
Upvotes: 2
Views: 13281
Reputation: 2615
Replace mongodb://localhost:27017
with mongodb://127.0.0.1:27017
To capture the exact error please follow the below approach.
const mongoose = require('mongoose');
const url = "mongodb://127.0.0.1:27017";
mongoose.connect(url).then(() => {
console.log("Connected to Database");
}).catch((err) => {
console.log("Not Connected to Database ERROR! ", err);
});
Upvotes: 4
Reputation: 1010
Your mongoose.connect
call is working fine. If it wasn't then you would definitely have received a PromiseRejection
for connection failure and a deprecated warning for UnhandledPromiseRejection
.
Still you can ensure that by adding few event listeners on mongoose events.
mongoose.connect('mongodb://127.0.0.1:27017');
mongoose.connection.on('connected', () => console.log('Connected'));
mongoose.connection.on('error', () => console.log('Connection failed with - ',err));
Coming to your error. This is more likely to happen when you have passed anything but a function as your handler to your app.use
or router.use
call. app.use
and router.use
both require functions to be passed to them which later express
would call whenever a request arrives.
The import statements on top of your code, where you require your routers are most likely to be the culprits here as by default every module.exports
is an object.
I need to look into your router files to further dig into the problem, but you may yourself verify the same. Just see if the module.exports
of every router file imported points to a express router instance - express.Router()
. This way every route file will export a configured express.Router()
instance which will be a function attached to app via app.use()
call.
Upvotes: 5