gpt4 book ai didi

javascript - 表达类型错误: undefined is not a function

转载 作者:行者123 更新时间:2023-12-02 15:54:41 24 4
gpt4 key购买 nike

我对 Node 和 Express 很菜鸟,当我运行我的应用程序时,出现此错误。有什么解决办法吗?我认为错误在这里“app.use(express.static(user));”但我不确定。

var express = require('express');
var http = require('http');
var path = require('path');

var app = express();

// modulos
var home = require('./controllers/home');
var model = require('./models');
var user = require('./controllers/user');

// all environments
app.set('port', process.env.PORT || 3005);
app.set('views', __dirname + '/layouts');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(user));

// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}

// rutas
app.use(home);

错误

express 500 TypeError:未定义不是函数

at Object.posix.isAbsolute (path.js:479:15)
at posix.normalize (path.js:461:26)
at SendStream.root.SendStream.from (/Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/send/lib/send.js:116:16)
at Object.staticMiddleware [as handle] (/Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/connect/lib/middleware/static.js:79:8)
at next (/Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at resume (/Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/connect/lib/middleware/static.js:60:7)
at SendStream.error (/Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/connect/lib/middleware/static.js:73:37)
at SendStream.emit (events.js:107:17)
at SendStream.error (/Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/send/lib/send.js:147:51)
at SendStream.onStatError (/Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/send/lib/send.js:248:48)
at /Applications/XAMPP/xamppfiles/htdocs/gestor/angular/jordi/node_modules/express/node_modules/send/lib/send.js:320:26
at FSReqWrap.oncomplete (fs.js:95:15)

controllers/user/index.js

// JavaScript Document
var express = require('express');
var app = module.exports = express();

app.set('views', __dirname + '/views');

app.get('/user/new', function(request, response) {

response.render('new');

});

最佳答案

不需要该行(app.use(express.static(user));) 将其删除。

您正在 controller/user/index.js 中定义新应用。

您设置的那些选项不会设置到主应用程序。更改代码如下。

主服务器.js

var user = require('./controllers/user')(app);  

Controller /用户/index.js

// JavaScript Document
module.exports = function (app){

app.set('views', __dirname + '/views');

app.get('/user/new', function(request, response) {

response.render('new');

});
}

关于javascript - 表达类型错误: undefined is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31651225/

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