(“ Router.use()需要中间件功能,但得到了'+ gettype(fn))

时间:2018-11-17 19:28:53

标签: node.js express

我创建了一个快速应用程序,但遇到了一些大问题:

  1. 我的路由太可怕了,无法将我的“登录”和“注册”页面连接到我的主页。一些建议会很有帮助。 (我已经附上了我的树状结构的图片)

  2. 我也遇到了错误-抛出新的TypeError('Router.use()需要中间件功能,但得到了'+ gettype(fn))

我的app.js

    // define dependencies
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var passport = require('passport');
var ejs = require('ejs');
var ExpressValidator = require('express-validator');
var LocalStrategy = require('passport-local').Strategy;
var multer = require('multer');
//handle file uploads
var upload = multer({des: './uploads'});
var flash = require('connect-flash');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var db = mongoose.connection;

// const PORT = 5500; // you can change this if this port number is not available
const router = express.Router();

var routes = require('./routes/index');
var users = require('./routes/users');

// app.use('/', routes);
// app.use('/users', users);

var app = express();

//view engine setup
app.use('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));


app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());


// Handle Sessions
app.use(session({
  secret: 'secret',
  saveUninitialized: true,
  resave: true
}));

//passport middleware
app.use(passport.initialize());
app.use(passport.session());

//Express Validator middleware
app.use(ExpressValidator({
  errorFormatter: function(param, msg, value){
    var namespace = param.split('.'),
    root = namespace.shift(),
    formParam = root;

  while(namespace.length){
    formParam += '[' + namespace.shift() + ']';
  }
  return {
    param : formParam,
    msg : msg,
    value : value
  };
  }
}));

//express messages middleware
app.use(require('connect-flash')());
app.use(function (req, res, next) {
  res.locals.messages = require('express-messages')(req, res);
  next();
});

//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 handlers

//development  error handler
//will print stacktrace
if (app.get('env') === 'development'){
  app.use(function(err, req, res, next){
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

//production erro handler
//no stacktraces leaked to user
app.use(function(err, req, res, next){
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

// //connect to database
// mongoose.connect('mongodb://localhost:27017/auth_tuts', { //replace this with you
//   // useMongoClient: true
// }, (err, db) => {
//   if (err) {
//     console.log("Couldn't connect to database");
//   } else {
//     console.log(`Connected To Database`);
//   }
// }
// );


module.exports = app;

我的user.js

var express = require('express');
const router = express.Router();

// GET home page. 
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});

router.get('/signup', function (req, res, next) {
    res.render('signup');
});

module.exports = router;

我的index.js

var express = require('express');[enter image description here][1]
var router = express.Router();

// GET home page. 
router.get('/', function(req, res, next){
    res.render('index', { title: 'Express' });
});

module.exports = router;

我的package.json文件

   {
  "name": "project",
  "version": "1.0.0",
  "description": "College Project",
  "main": "app.js",
  "scripts": {
    "start": "node ./bin/www"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/KevinKerin/kjs-webdesign.git"
  },
  "author": "Johnathan Munster",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/KevinKerin/kjs-webdesign/issues"
  },
  "homepage": "https://github.com/KevinKerin/kjs-webdesign#readme",
  "dependencies": {
    "bcrypt": "^3.0.2",
    "body-parser": "^1.18.3",
    "connect-flash": "*",
    "cookie-parser": "*",
    "debug": "*",
    "ejs": "^2.6.1",
    "express": "^4.16.4",
    "express-messages": "*",
    "express-session": "^1.15.6",
    "express-validator": "*",
    "jsonwebtoken": "^8.4.0",
    "mongodb": "*",
    "mongoose": "^5.3.12",
    "morgan": "*",
    "multer": "*",
    "nodemailer": "^4.6.8",
    "nodemailer-smtp-transport": "^2.7.4",
    "passport": "*",
    "passport-http": "*",
    "passport-local": "*",
    "serve-favicon": "*",
    "shortid": "^2.2.14"
  }
}

文件结构

1 个答案:

答案 0 :(得分:0)

问题已解决。我有.use而不是.set(‘views’, path...)

正在处理我的路由。