我正在学习带有快速模板引擎的node.js,正在遵循udemy课程“通过构建10个项目来学习node.js”,而在讲座中,当教授npm start localhost:3000
开始运行时,我的弹出错误指示 app.use需要 我尝试过的 中间件功能
匹配的代码及其相同。请帮助我解决错误,我已经在这里停留了几个小时,尝试了许多修改,但对我不起作用。
当我尝试运行“ npm start”时,弹出以下错误
TypeError:app.use()需要中间件功能
这是app.js代码:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var expressValidator = require('express-validator');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var bodyParser = require('body-parser');
var multer = require('multer');
var flash = require('connect-flash');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var db = mongoose.connection;
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
//Handle File Uploads
app.use(multer({
dest: './uploads'
}));
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
//Handle Express Sessions
app.use(session({
secret: 'secret',
saveUninitialied: true,
resave: true
}));
//Passport
app.use(passport.initialize());
app.use(passport.session());
//Validator
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
};
}
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(flash());
app.use(function(req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
app.use('/', routes);
app.use('/users', users);
// 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 error 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: {}
});
});
module.exports = app;
这是package.json文件
{
"name": "nodeauth",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.12.4",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"express": "~4.12.4",
"jade": "~1.9.2",
"morgan": "~1.5.3",
"serve-favicon": "~2.2.1",
"mongodb": "*",
"mongoose": "*",
"connect-flash": "*",
"express-validator": "*",
"express-session": "*",
"express-messages": "*",
"passport": "*",
"passport-local": "*",
"passport-http": "*",
"multer": "*"
}
}
这是更新的app.js(删除Multar之后)
var express = require('express'),
path = require('path'),
favicon = require('serve-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
expressValidator = require('express-validator'),
session = require('express-session'),
passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
bodyParser = require('body-parser'),
flash = require('connect-flash'),
mongo = require('mongodb'),
mongoose = require('mongoose'),
db = mongoose.connection,
routes = require('./routes/index'),
users = require('./routes/users'),
app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// handle file uploads
//multer delted
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// handle express sessions
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
}));
// passport
app.use(passport.initialize());
app.use(passport.session());
// validator
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
};
}
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// flash
app.use(flash());
// express messages
app.use(function(req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
app.get('*', function(req, res, next) {
res.locals.user = req.user || null;
next();
});
app.use('/', routes);
app.use('/users', users);
// 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 error 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: {}
});
});
//Express server
var http = require('http');
var server = http.createServer(app);
app.get('/', function(req, res) {
res.send("Hello World!");
});
server.listen(3000, 'localhost');
server.on('listening', function() {
console.log('Express server started on port %s at %s', server.address().port, server.address().address);
});
module.exports = app;
谢谢
它以这种方式工作。 删除这一行,
app.use(multer({ dest: './uploads' }));
并用作
var multer = require('multer');
var upload = multer({ dest: './uploads' });
我也遵循相同的路线
请看一下这段代码。最后一行出现了一个错误,因为我传递的是一个“索引”而不是一个资源。我以为是皮毛的问题,试图压制它。然后我注意到只有在构建发布时才会出现这个错误。它在构建调试时工作良好。我完全一无所知。谁能告诉我我做错了什么。
我们有一个实体的层次结构,例如,抽象Basket和两个concrete,它们又可以包含一些其他项的列表,例如,具有完全相同的层次结构(SaleBasketItem,ReturnBasketItem)。 因此,当我尝试执行hql查询时,问题就出现了 其中动态地为“sale”或“return”。然后,我将该查询放入中,其类型为我需要的,并尝试,其中是表示混凝土篮状态的一些枚举(每个混凝土类都不同,在抽
我有一个问题需要帮助,我有下面的python脚本,并尝试将值传递给DateTime.date。 这返回 现在,我要将传递给。但是,我得到了一个错误: 错误:
问题内容: 新版本的express中对中间件进行了一些更改,并且围绕该问题的其他一些帖子,我对代码进行了一些更改,但我束手无策。 我们已经事先进行了工作,但是我不记得发生了什么变化。 app.js 似乎由于版本控制原因,此问题的答案已更改。感谢Nik 问题答案: 如果您使用的是高于2.x的express,则必须像以下代码一样声明app.router。请尝试替换您的代码 与 谢谢 注意: app.r
/*//UserProfile NavDraweritems.add(new NavDrawerItem(NavMenutitles[1],NavMenuicons.getResourceId(1,-1)));*///Payment NavDraweritems.add(new NavDrawerItem(NavMenutitles[1],NavMenuicons.getResourceId(1,