你好,我正在玩NodeJS和expressJS,实际上我在看教程,并在编辑器中编写了相同的代码,但是在视频中,他得到的结果非常完美,但是在我的机器上,当我在其中运行命令时,它不起作用浏览器http://localhost:3000/无法正常工作,谁能帮助我解决问题所在? 预先谢谢你。
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var expressValidator = require('express-validator');
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 indexRouter = require('./routes/index');
var usersRouter = 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'}));
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// handle express sessions
app.use(session({
secret:'secret',
saveUninitialized: true,
resave:true
}));
//passport
app.use(passport.initialize());
app.use(passport.session());
//validator
app.use(expressValidator({
errorFormatter: function(pram, msg, value){
var namespace = pram.split('.')
, root = namespace.shift()
, formPram = root;
while(namespace.length){
formPram += '[' + namespace.shift() + ']';
}
return {
pram: formPram,
msg : msg,
value : value
};
}
}));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use(flash());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;