node+express+ejs开发简单的web应用,为什么localhost可以访问,然而部署在服务器上就404,其中可能出现的问题有哪些?
本地环境: node :v6.3.1 服务器: node:v4.3.2 版本不同是否会导致这种情况?
var routes = require('./routes/index');
var tools = require('./routes/tools');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//app.engine('hbs', exphbs({defaultLayout: 'layout'}));
//hbs.registerPartials(__dirname + '/views/v2/partials');
// uncomment after placing your favicon in /public
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/api', expressJwt({secret: config.secret}));
app.use('/v2/api', expressJwt({secret: config.secret}));
app.use('/', routes);
app.use('/tools', tools);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
console.log("111111")
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);
if (err.status === 404) {
res.render('404', {title: '404', min: Utils.min()});
return;
}
res.render('error', {
message: err.message,
error: err,
min: Utils.min()
});
next()
});
}
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
res.status(err.status || 500);
if (err.status === 404) {
res.render('404', {title: '404', min: Utils.min()});
return;
}
res.render('error', {
message: err.message,
error: {},
min: Utils.min()
});
next()
});
module.exports = app;
以上是路由代码,下面是出现404的 页面代码
var express = require('express');
var router = express.Router();
var Utils = require('../lib/util');
router.get('/test', function (req, res) {
res.render('test', {title: 'welcome', min: Utils.min()});
});
router.get('/transform', function (req, res) {
res.render('transform', {title: '工具箱', min: Utils.min()});
});
module.exports = router;
希望大神帮忙看看问题出在哪?
1 回复
监听的端口是什么, 看下有什么报错