我在尝试使我的代码正常工作时遇到了一些问题。我已经尝试了几件事,但是它说我的错误在第28行:“ app.use('/','indexRouter')”,但我不知道为什么。我的index.js文件和app.js文件是彼此的副本。
我的app.js文件:
let express = require('express')
let path = require('path')
let favicon = require('serve-favicon')
let cookieParser = require('cookie-parser')
let bodyParser = require('body-parser')
let logger = require('morgan')
//Starts an express app
let app = express()
//Gives access to routes
//You will seperate each route base on different activites and group them that way
let indexRouter = require('./routes/index')
let userRouter = require('./routes/users')
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router);
routes.initialize(app);
app.use('/','indexRouter')
app.use('/users','userRouter')
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
//ALlows www to get access to it
module.exports = app
index.js文件:
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('index js stuff');
});
module.exports = router;
答案 0 :(得分:1)
indexRouter
和userRouter
不应为字符串。
app.use('/', indexRouter)
app.use('/users', userRouter)