0
我認爲這是一個緩存的問題後,但之後的所有代碼我的服務器上的三個完整清除,我還是遇到了同樣的問題:Express.js繼續顯示「未找到」刪除默認代碼
快遞發電機自帶的默認的錯誤在處理app.js文件:
// 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 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.redirect('/');
});
因爲我已經換成我自己捕獲的所有路由代碼,使用app.all:
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('/', index);
app.use('/users', users);
// catch 404 and forward to error handler (catch-all)
app.all('*', function(req, res) {
res.render('index.html');
});
請記住render語句的工作原理。我正在使用單頁面應用程序,我需要將其重定向回index.html文件。
這實際上在本地工作,但不能在我的服務器上工作。該服務器是一個Node.js服務器。
404錯誤的默認消息是「未找到」。無論我多次刪除此代碼和404錯誤消息,我都會在瀏覽器中看到「未找到」(Express.js樣式),而不是重定向到我的主頁。
由於問題是與「我的服務器」,你可以詳細說明一點。但是,無論如何,我認爲一般情況下,您必須確保node.js進程正在重新啓動,並且您已經清除了服務器的緩存(如果可能的話)? –