设置error404处理程序时,页面不起作用(app.get('*',......))

时间:2016-11-30 17:38:33

标签: node.js express routing routes url-routing

我正在尝试设置error404处理程序。正如我在这里看到的那样,我应该在其余路线的最后一步将'*'路径设置为error404.jade页面。

执行此操作时,页面不会加载图像或css或javascript文件。这对我来说没有意义。

app.ts

import express = require('express');
import routes = require('./routes/index');
import http = require('http');
import path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);

app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
    app.use(express.errorHandler());
}

app.get('/', routes.login);
app.get('/home', routes.home);
app.get('/markets', routes.markets);
app.get('/market/*', routes.specific_market);
app.get('/trades', routes.trades);
app.get('/about', routes.about);
app.get('/contact', routes.contact);
//app.get('*', routes.error404);

http.createServer(app).listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
});

index.ts

/*
 * GET home page.  
 */
import express = require('express');
import market_routes = require('./market-routes');
import trade_routes = require('./trade-routes');
import data = require('../Scripts/data-test');

// STATIC PAGES
export function login(req: express.Request, res: express.Response) {
    res.render('login', { title: 'Login', user, year: new Date().getFullYear() });
};
export function about(req: express.Request, res: express.Response) {
    res.render('about', { title: 'About', user, year: new Date().getFullYear(), message: 'Description page' });
};
export function contact(req: express.Request, res: express.Response) {
    res.render('contact', { title: 'Contact', user, year: new Date().getFullYear(), message: 'Contact page' });
};
export function error404(req: express.Request, res: express.Response) {
    res.render('error404', { title: 'Not Found', user, year: new Date().getFullYear(), message: 'Error page' });
};

var user = data.user;

export function home(req: express.Request, res: express.Response) {
    res.render('home', { title: 'Home', user, year: new Date().getFullYear() });
};

export function markets(req: express.Request, res: express.Response) { market_routes.markets(req, res); };
export function specific_market(req: express.Request, res: express.Response) { market_routes.specific_market(req, res); };

export function trades(req: express.Request, res: express.Response) { trade_routes.trades(req, res); };
export function advanced_trades(req: express.Request, res: express.Response) { trade_routes.advanced_trades(req, res); };

(img) Page with the line 'uncommented'

(img) Page with the line 'commented'

1 个答案:

答案 0 :(得分:0)

你的静力学没找到。使用没有'*'的路线:

app.use(routes.error404);