gpt4 book ai didi

javascript - Node.js - 类型错误 : Cannot read property 'handle' of undefined

转载 作者:行者123 更新时间:2023-11-30 20:37:37 25 4
gpt4 key购买 nike

我有一些 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/'));

最佳答案

问题出在旧版本的 express.js 上。在线抛出错误

server.use('/article/',r1);

因为 r1 未定义。您需要将 express 版本升级到 4.x.x,因为路由器在 3.x.x 版本中不可用。您可以使用此命令执行此操作:

npm install express@4.13.4 --save

关于javascript - Node.js - 类型错误 : Cannot read property 'handle' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49645441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com