Reputation: 5
I am new to nodeJS and I was learning how to create a simple api app which connects to mongoDB.
I read a few blogs and studied a few simple projects, I started to make it on my own but there was a slight issue. I followed all the steps and even checked solving the issue on my own.
Though, I wasnt successful the issue comes in server.js when I link it with api.js and throws the following error
D:\Node Js\nodeJs\userStory\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 undefined
at Function.use (D:\Node Js\nodeJs\userStory\node_modules\express\lib\router\index.js:458:13)
at Function.<anonymous> (D:\Node Js\nodeJs\userStory\node_modules\express\lib\application.js:220:21)
at Array.forEach (<anonymous>)
at Function.use (D:\Node Js\nodeJs\userStory\node_modules\express\lib\application.js:217:7)
at Object.<anonymous> (D:\Node Js\nodeJs\userStory\server.js:26: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
Would be helpful if anyone can have a look at it and guide me where I am going wrong.
//---Server.js
var express = require('express');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var config = require('./config');
var mongoose = require('mongoose');
var app = express();
mongoose.connect(config.database,function(err){
if(err){
console.log(err);
}else{
console.log("Connected to a database");
}
});
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(morgan('dev'));
app.use("/api",require('./app/route/api')(app,express));
app.get('*',function(req,res){
res.sendFile(__dirname + '/public/views/index.html');
});
app.listen(config.port,function(err){
if(err){
console.log(err);
}else{
console.log("Listening on port "+config.port);
}
});
//api.js
var user = require('../model/user');
var config = require('../../config');
var secretKey = config.secretKey;
module.exports = function(app,express){
var api = express.Router();
//passing the data to the DB
api.post('/signup',function(req,res){
var user = new User({
name:req.body.name,
username:req.body.username,
password:req.body.password
});//body here is the body parser for reading the value from the DB
//Now saving it on the DB
user.save(function(err){
if(err){
res.send(err);
return;
}
res.json({message:'User has been created'});
});
});
};
Upvotes: 0
Views: 3971
Reputation: 133
There are different cases when this error comes.
In my case, I got this error when code was as below
app.js
const { getRouter } = require('./routes/getAPIs');
...
expressServer.use("/", getRouter);
other.js
const router = expressServer.Router();
router.get("/", getData);
module.exports = router;
Here the problem is using destructing way of import instead of normal way when destructuring way is not used(We used module.exports method).
Solution for this is remove {} around the import object.
app.js
const getRouter = require('./routes/getAPIs');
...
expressServer.use("/", getRouter);
Upvotes: 3
Reputation: 253
You should export the router, not a function.
var api = express.Router();
//passing the data to the DB
api.post('/signup',function(req,res){
var user = new User({
name:req.body.name,
username:req.body.username,
password:req.body.password
});//body here is the body parser for reading the value from the DB
//Now saving it on the DB
user.save(function(err){
if(err){
res.send(err);
return;
}
res.json({message:'User has been created'});
});
});
module.exports = api;
Change your app.js accordingly
app.use("/api",require('./app/route/api'));
Upvotes: 3