我有一些提供此错误的Node.js代码
有人能告诉我这个错误意味着什么,我该怎么做才能解决它?
非常感谢!
if (fn.handle && fn.set) app = fn;
^
TypeError: Cannot read property 'handle' of undefined
at Function.app.use (/Users/sylvia/Documents/FE/Daily-
Learning/Node/final/node_modules/express/lib/application.js:110:9)
at Object.<anonymous> (/Users/sylvia/Documents/FE/Daily- Learning/Node/final/server.js:46:8)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:140:18)
at node.js:1043:3
代码:
var express = require('express');
var expressStatic = require('express-static');
var mysql = require('mysql');
var cookieParser = require('cookie-parser');
var cookieSession = require('cookie-session');
var bodyParser = require('body-parser');
var consolidate = require('consolidate');
var expressRoute = require('express-route');
var multer = require('multer');
var multerObj = multer({dest:'./static/upload'});
var server = express();
server.listen(8080);
server.use(multerObj.any());
server.use(cookieParser());
(function(){
var keys=[];
for(var i=0;i<100000;i++){
keys[i]='a_' + Math.random();
}
server.use(cookieSession({
name: 'sess_id',
keys: keys,
maxAge: 20*60*1000 //20mins
}));
})();
server.engine('html',consolidate.ejs);
server.set('views','template');
server.set('view engine','html');
var r1 = express.Router();
var r2 = express.Router();
server.use('/article/',r1);
r1.get('/1.html',(req,res)=>{
res.send('aaa').end();
});
r1.get('/2.html',(req,res)=>{
res.send('bbb').end();
});
server.use('/blog/',r2);
r2.get('/a.html',(req,res)=>{
res.send('ccc').end();
});
r2.get('/b.html',(req,res)=>{
res.send('ddd').end();
});
server.use(expressStatic('./static/'));
答案 0 :(得分:1)
问题在于旧版本的express.js。该错误将在线上抛出
server.use('/article/',r1);
因为r1
未定义。您需要将Express版本升级到4.x.x,因为路由器在3.x.x版本中不可用。您可以使用此命令执行此操作:
npm install express@4.13.4 --save