将Express默认路由重定向到新路由

时间:2018-04-21 18:44:35

标签: express handlebars.js feathersjs express-handlebars

我有FeathersJS应用程序(位于Express之上),我使用把手作为模板引擎。

我想将express的'/'重定向到其他URL但是继续加载index.html在/ public文件夹中。我的目标是将其重定向到车把页面,以便我可以使用布局而不要重复自己。

const path = require('path');
const favicon = require('serve-favicon');
const compress = require('compression');
const cors = require('cors');
const helmet = require('helmet');
const logger = require('winston');

const feathers = require('@feathersjs/feathers');
const configuration = require('@feathersjs/configuration');
const express = require('@feathersjs/express');
const exphbs = require('express-handlebars');
const socketio = require('@feathersjs/socketio');


const middleware = require('./middleware');
const services = require('./services');
const appHooks = require('./app.hooks');
const channels = require('./channels');

const app = express(feathers());

app.set('views', path.join(app.get('views')));
app.engine('.hbs', exphbs({
    extname: '.hbs',
    defaultLayout: 'main'
}));
app.set('view engine', '.hbs');

// Load app configuration
app.configure(configuration());
// Enable CORS, security, compression, favicon and body parsing
app.use(cors());
app.use(helmet());
app.use(compress());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(favicon(path.join(app.get('public'), 'favicon.ico')));
app.use(express.static(app.get('public')));
// Host the public folder

app.get('/', function(req, res){
    console.log('test');
    res.redirect('/home');
});

app.get('/home', function(req, res, next){
    res.render('home');
});
app.get('/test', function(req, res, next){
    res.redirect('/');
});
app.get('/admin', function(req, res, next) {
    res.render('admin',{});
});
app.get('/services', function(req, res, next) {
    res.render('services',{});
});


// Set up Plugins and providers
app.configure(express.rest());
app.configure(socketio());

// Configure other middleware (see `middleware/index.js`)
app.configure(middleware);
// Set up our services (see `services/index.js`)
app.configure(services);
// Set up event channels (see channels.js)
app.configure(channels);

// Configure a middleware for 404s and the error handler
app.use(express.notFound());
app.use(express.errorHandler({ logger }));

app.hooks(appHooks);

module.exports = app;

我指出它不会'/ home'。我错过了什么吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

这是因为您使用express.static中间件之前 /处理程序,因为公共文件夹中有index.html,当您点击{{ 1}}它默认为索引文件,您可以像这样切换顺序:

/