I'm trying to setup a node project and I want to put a file routes.js
in routes/routes.js
and controllers files in controllers/
directory.
So, for example I have the UserController
like this:
var index = () =>
{
console.log("User Index");
};
var getUser = (id) => {
console.log("User by id " + id);
};
module.exports =
{
index,
getUser
}
And in routes.js
I have this:
var express = require('express');
var routes = express.Router();
var users = require('../controllers/usersController');
routes.route('/')
.get(users.index);
routes.route('/user/:userId')
.get(users.getUser);
module.exports=
{
routes
};
And in index.js
I'm setup in this way:
let express = require('express');
let app = express();
let routes = require('./routes/routes');
app.set("views", './views');
app.set("view engine", 'jade');
app.use(express.static(__dirname + '/public'));
app.use('/', routes.index);
// launch ======================================================================
app.listen(9001);
When I try to run the server I have this error:
.../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
What is the problem how can I configure setup this in this way?
Thank you
I'm trying to setup a node project and I want to put a file routes.js
in routes/routes.js
and controllers files in controllers/
directory.
So, for example I have the UserController
like this:
var index = () =>
{
console.log("User Index");
};
var getUser = (id) => {
console.log("User by id " + id);
};
module.exports =
{
index,
getUser
}
And in routes.js
I have this:
var express = require('express');
var routes = express.Router();
var users = require('../controllers/usersController');
routes.route('/')
.get(users.index);
routes.route('/user/:userId')
.get(users.getUser);
module.exports=
{
routes
};
And in index.js
I'm setup in this way:
let express = require('express');
let app = express();
let routes = require('./routes/routes');
app.set("views", './views');
app.set("view engine", 'jade');
app.use(express.static(__dirname + '/public'));
app.use('/', routes.index);
// launch ======================================================================
app.listen(9001);
When I try to run the server I have this error:
.../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
What is the problem how can I configure setup this in this way?
Thank you
Share Improve this question asked Oct 31, 2017 at 11:52 user3242861user3242861 1,92912 gold badges53 silver badges97 bronze badges 2- did you copy paste this code? – Marco Salerno Commented Oct 31, 2017 at 11:54
-
In
routes.js
you never export anything calledindex
, and thus in the lineapp.use('/', routes.index)
inside ofindex.js
,routes.index
is undefined. You do however export something calledroutes
(as inroutes.routes
), so maybe that's what you meant to use? – Khauri Commented Oct 31, 2017 at 11:57
2 Answers
Reset to default 4Your userController will be like this.
module.exports = {
index: (req, res) => {
console.log("User Index");
},
getUser: (req, res) => {
console.log("User by id " + req.params.id);
}
}
Your routes file will be like this
var express = require('express');
var routes = express.Router();
var users = require('../controllers/usersController');
routes.get('/', user.index);
routes.get('/user/:userId', user.getUser);
module.exports = routes;
Your index file will be like this
let express = require('express');
let app = express();
let routes = require('./routes/routes');
app.set("views", './views');
app.set("view engine", 'jade');
app.use(express.static(__dirname + '/public'));
app.use('/', routes);
app.listen(9001);
In routes.js
replace:
module.exports=
{
routes
};
to
module.exports = routes;
in index.js
app.use('/', routes.index);
to app.use('/', routes);
should works...
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745346973a4623595.html
评论列表(0条)