admin管理员组文章数量:1308171
I would appreciate some help with this please. Not certain exactly sure what to this means as this is my first time working with node and express. I set up express to use with node, and tried to follow the information on the site Express.js . Would appreciate some help understanding what I may be missing here please.
...\node_modules\express\lib\application.js:178
if (fn.handle && fn.set) mount_app = fn;
^
TypeError: Cannot read property 'handle' of undefined
at Function.app.use (....\node_modules\express\lib\application.js:178:9)
at Object.<anonymous> (....\app.js:18:5)
at Module._pile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
/**
* Module dependencies.
*/
var http = require('http');
//var express = require('../..');
var module = require("module")
var logger = require('morgan');
var express = require('express');
var app = module.exports = express();
var silent = 'test' == process.env.NODE_ENV;
var httpServer = http.createServer(app);
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
// app middleware
app.use(express.static(__dirname + '/public'));
app.use(bodyParser());
app.use(methodOverride());
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);
api.use(logger('dev'));
api.use(bodyParser());
/**
* CORS support.
*/
api.all('*', function(req, res, next)
{
if (!req.get('Origin')) return next();// use "*" here to accept any origin
res.set('Access-Control-Allow-Origin', 'http://localhost:3000');
res.set('Access-Control-Allow-Methods', 'GET, POST');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
res.set('Access-Control-Allow-Max-Age', 3600);
if ('OPTIONS' == req.method) return res.send(200);
next();
});
// middleware with an arity of 4 are considered error handling middleware. When you next(err)
// it will be passed through the defined middleware in order, but ONLY those with an arity of 4, ignoring regular middleware.
var clientErrorHandler=function(err, req, res, next) {
if (req.xhr) {// whatever you want here, feel free to populate properties on `err` to treat it differently in here.
res.send(err.status || 500, { error: err.message });
}
else
{ next(err);}
};
// create an error with .status. we can then use the property in our custom error handler (Connect repects this prop as well)
var error=function (status, msg) {
var err = new Error(msg);
err.status = status;
return err;
};
var logErrors=function (err, req, res, next) {
console.error(err.stack);
next(err);
};
var errorHandler=function (err, req, res, next) {
res.status(500);
res.render('error', { error: err });
};
// general config
app.set('views', __dirname + '/views');
//app.set('view engine', 'jade');
// our custom "verbose errors" setting which we can use in the templates via settings['verbose errors']
app.enable('verbose errors');// disable them in production use $ NODE_ENV=production node examples/error-pages
if ('production' == app.settings.env) {app.disable('verbose errors');}
silent || app.use(logger('dev'));
// Routes
app.get('/404', function(req, res, next){
next();// trigger a 404 since no other middleware will match /404 after this one, and we're not responding here
});
app.get('/403', function(req, res, next){// trigger a 403 error
var err = new Error('not allowed!');
err.status = 403;
next(err);
});
app.get('/500', function(req, res, next){// trigger a generic (500) error
next(new Error('keyboard cat!'));
});
// Error handlers
// Since this is the last non-error-handling middleware use()d, we assume 404, as nothing else responded.
// $ curl http://localhost:3000/notfound
// $ curl http://localhost:3000/notfound -H "Accept: application/json"
// $ curl http://localhost:3000/notfound -H "Accept: text/plain"
app.use(function(req, res, next){
res.status(404);
if (req.accepts('html')) {// respond with html page
res.render('404', { url: req.url });
return;
}
if (req.accepts('json')) {// respond with json
res.send({ error: 'Not found' });
return;
}
res.type('txt').send('Not found');// default to plain-text. send()
});
// error-handling middleware, take the same form as regular middleware, however they require an
// arity of 4, aka the signature (err, req, res, next).when connect has an error, it will invoke ONLY error-handling middleware.
// If we were to next() here any remaining non-error-handling middleware would then be executed, or if we next(err) to
// continue passing the error, only error-handling middleware would remain being executed, however here
// we simply respond with an error page.
app.use(function(err, req, res, next){
// we may use properties of the error object here and next(err) appropriately, or if we possibly recovered from the error, simply next().
res.status(err.status || 500);
res.render('500', { error: err });
});
if (!module.parent) {// assigning to exports will not modify module, must use module.exports
app.listen(3000);
silent || console.log('Express started on port 3000');
};
I would appreciate some help with this please. Not certain exactly sure what to this means as this is my first time working with node and express. I set up express to use with node, and tried to follow the information on the site Express.js . Would appreciate some help understanding what I may be missing here please.
...\node_modules\express\lib\application.js:178
if (fn.handle && fn.set) mount_app = fn;
^
TypeError: Cannot read property 'handle' of undefined
at Function.app.use (....\node_modules\express\lib\application.js:178:9)
at Object.<anonymous> (....\app.js:18:5)
at Module._pile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
/**
* Module dependencies.
*/
var http = require('http');
//var express = require('../..');
var module = require("module")
var logger = require('morgan');
var express = require('express');
var app = module.exports = express();
var silent = 'test' == process.env.NODE_ENV;
var httpServer = http.createServer(app);
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
// app middleware
app.use(express.static(__dirname + '/public'));
app.use(bodyParser());
app.use(methodOverride());
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);
api.use(logger('dev'));
api.use(bodyParser());
/**
* CORS support.
*/
api.all('*', function(req, res, next)
{
if (!req.get('Origin')) return next();// use "*" here to accept any origin
res.set('Access-Control-Allow-Origin', 'http://localhost:3000');
res.set('Access-Control-Allow-Methods', 'GET, POST');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
res.set('Access-Control-Allow-Max-Age', 3600);
if ('OPTIONS' == req.method) return res.send(200);
next();
});
// middleware with an arity of 4 are considered error handling middleware. When you next(err)
// it will be passed through the defined middleware in order, but ONLY those with an arity of 4, ignoring regular middleware.
var clientErrorHandler=function(err, req, res, next) {
if (req.xhr) {// whatever you want here, feel free to populate properties on `err` to treat it differently in here.
res.send(err.status || 500, { error: err.message });
}
else
{ next(err);}
};
// create an error with .status. we can then use the property in our custom error handler (Connect repects this prop as well)
var error=function (status, msg) {
var err = new Error(msg);
err.status = status;
return err;
};
var logErrors=function (err, req, res, next) {
console.error(err.stack);
next(err);
};
var errorHandler=function (err, req, res, next) {
res.status(500);
res.render('error', { error: err });
};
// general config
app.set('views', __dirname + '/views');
//app.set('view engine', 'jade');
// our custom "verbose errors" setting which we can use in the templates via settings['verbose errors']
app.enable('verbose errors');// disable them in production use $ NODE_ENV=production node examples/error-pages
if ('production' == app.settings.env) {app.disable('verbose errors');}
silent || app.use(logger('dev'));
// Routes
app.get('/404', function(req, res, next){
next();// trigger a 404 since no other middleware will match /404 after this one, and we're not responding here
});
app.get('/403', function(req, res, next){// trigger a 403 error
var err = new Error('not allowed!');
err.status = 403;
next(err);
});
app.get('/500', function(req, res, next){// trigger a generic (500) error
next(new Error('keyboard cat!'));
});
// Error handlers
// Since this is the last non-error-handling middleware use()d, we assume 404, as nothing else responded.
// $ curl http://localhost:3000/notfound
// $ curl http://localhost:3000/notfound -H "Accept: application/json"
// $ curl http://localhost:3000/notfound -H "Accept: text/plain"
app.use(function(req, res, next){
res.status(404);
if (req.accepts('html')) {// respond with html page
res.render('404', { url: req.url });
return;
}
if (req.accepts('json')) {// respond with json
res.send({ error: 'Not found' });
return;
}
res.type('txt').send('Not found');// default to plain-text. send()
});
// error-handling middleware, take the same form as regular middleware, however they require an
// arity of 4, aka the signature (err, req, res, next).when connect has an error, it will invoke ONLY error-handling middleware.
// If we were to next() here any remaining non-error-handling middleware would then be executed, or if we next(err) to
// continue passing the error, only error-handling middleware would remain being executed, however here
// we simply respond with an error page.
app.use(function(err, req, res, next){
// we may use properties of the error object here and next(err) appropriately, or if we possibly recovered from the error, simply next().
res.status(err.status || 500);
res.render('500', { error: err });
});
if (!module.parent) {// assigning to exports will not modify module, must use module.exports
app.listen(3000);
silent || console.log('Express started on port 3000');
};
Share
Improve this question
edited May 11, 2014 at 4:47
mscdex
107k15 gold badges200 silver badges158 bronze badges
asked May 11, 2014 at 1:38
KobojunkieKobojunkie
6,55532 gold badges112 silver badges164 bronze badges
4 Answers
Reset to default 2The problem is that you're trying to use()
a variable that isn't defined yet. If you use the function logErrors(err, req, res, next) {
syntax instead of var logErrors=function (err, req, res, next) {
, your TypeErrors should go away.
Try to move var clientErrorHandler, errorHandler and etc before your call app.use for this.
Like:
var errorHandler = function (req, res, next) { .... }
app.use(errorHandler);
Thanks i also had the same issue i was using doing this
app.use(__dirname+'path');
Instead of doing
app.use(express.static(__dirname+'/path'));
Agh ! small little typos .
For those having issue in react app
Just remove the express from your node modules and reinstall it again
For that Type In Your terminal in the project directory that you are working on:
npm uninstall express
npm install express
本文标签:
版权声明:本文标题:javascript - TypeError: Cannot read property 'handle' of undefined --- if (fn.handle && fn.set) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741852473a2401146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论