const express = require("express"),
http = require("http"),
bodyParser = require("body-parser"),
cookieParser = require('cookie-parser'),
session = require('express-session'),
morgan = require('morgan'),
methodOverride = require('method-override'),
compression = require('compression');
const app = express();
// compress responses
app.use(compression());
process.on('uncaughtException', function(exception) {
console.log(exception);
});
const config = require("./app/config.js")();
const logger = require("./app/server/modules/logger");
//app Config.
app.use(methodOverride());
app.set('env', 'dev');
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
//cross domain
app.use(function(req, res, next) {
if (req.method === 'OPTIONS') {
var headers = {};
headers["Access-Control-Allow-Origin"] = "*";
headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
headers["Access-Control-Allow-Credentials"] = false;
headers["Access-Control-Max-Age"] = '86400'; // 24 hours
headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept";
res.writeHead(200, headers);
res.end();
}
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");
res.header["Access-Control-Allow-Credentials"] = false;
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With,X-HTTP-Method-Override, Content-Type, Accept");
next();
});
app.use(cookieParser());
app.use(session({ secret: 'xxxxxxxxxxx' }));
// app.use(cookieParser('optional secret string'));
// app.use(session({secret: 'xxxxxxxxxxx'}));
app.locals.pretty = true;
//Folder config.
/*app.use(express.static(__dirname + '/app/user/'));*/
app.use('/', express.static(__dirname + '/app/user/'));
app.use('/admin', express.static(__dirname + '/app/admin/'));
require('./app/server/router')(app); //route file
// catch 404 and forward to error handler
app.use(function(req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
if (app.get('env') === 'dev') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({
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.html', {
message: err.message,
error: {}
});
});
http.createServer(app).listen(config.port, function(err) {
if (err) console.log(err);
logger.info("server listening on port " + config.port + " in " +
config.mode + " mode");
});
这是我的http链接http://localhost:9989/,稍后它将重新定向到http://localhost:9989/login,这是第一次正常工作,当我刷新http://localhost:9989/login时,它会显示{“message”:“Not Found” ,“错误”:{“状态”:404}},为什么会发生?这有什么解决方案吗?或者我有任何错误吗?请指导我!!