简体   繁体   English

节点/ Express:根“ /”路由的路由标识符

[英]Node/Express: Route identifiers for root '/' route

I'm developing a directory app where content is rendered server-side depending on some parameters. 我正在开发一个目录应用程序,其中根据某些参数在服务器端呈现内容。 My directory is a subapp of other app that is running on appache. 我的目录是在appache上运行的其他应用程序的子应用程序。 I use the reverse proxy to redirect to node when needed. 我在需要时使用反向代理重定向到节点。

So, my node root route is this: http://myapp.com/directory/ 因此,我的节点根路由是这样的: http://myapp.com/directory/ : http://myapp.com/directory/

I need to have routes in this format: http://maypp.com/directory/location/category 我需要采用以下格式的路由: http://maypp.com/directory/location/category : http://maypp.com/directory/location/category

where, location and category can be anything! 哪里,位置和类别可以是任何东西! (it's user generated content) (这是用户生成的内容)

My initial thought was that I would be able to do something like this: 我最初的想法是,我将可以执行以下操作:

app.get('/:location?/:category?', routes.index);

However, I get the following behavior: In my routes.index after I render the view I have console.log('location: ' + req.params.location + ', category: ' + req.params.category); 但是,我得到以下行为:渲染视图后,在routes.index具有console.log('location: ' + req.params.location + ', category: ' + req.params.category);

When I navigate to http://maypp.com/directory/ I get the following: 当我导航到http://maypp.com/directory/我得到以下信息:

location: css, category: main.css
GET /components/modernizr/modernizr.js 200 69ms - 48.97kb
GET /components/requirejs/require.js 200 69ms - 80.75kb

And browser output is broken with the following error: 并且浏览器输出中断,并出现以下错误:

Uncaught SyntaxError: Unexpected token < main.js:1

And the weirdest thing is that main.js contains rendered HTML page instead of my browser-side js modules. 最奇怪的是main.js包含呈现的HTML页面,而不是我的浏览器端js模块。

Another test case: When I navigate to http://maypp.com/directory/loc/cat I get the following: 另一个测试用例:当我导航到http://maypp.com/directory/loc/cat ,得到以下信息:

location: loc, category: cat
location: css, category: main.css
GET /components/modernizr/modernizr.js 200 6ms - 48.97kb
GET /components/requirejs/require.js 200 6ms - 80.75kb
location: js, category: main.js

and same stuff as above on the browser's side. 和与浏览器相同的内容。

In my layout.jade I have the following: 在我的layout.jade我有以下内容:

script(type="text/javascript", src="/directory/components/requirejs/require.js" data-main="/directory/js/main")

If I have my route defined without second identifier (like this app.get('/:location?', routes.index); ), it works fine, but it's not what I need! 如果我定义的路线没有第二个标识符(例如app.get('/:location?', routes.index); ),则可以正常工作,但这不是我所需要的!

====== Answer ========= ======答案=========
Moved all of the route handling below static asset handling middleware. 将所有路由处理都移至静态资产处理中间件下方。

var app = express();

app.set('port', process.env.PORT || 3000);
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.set('view options', {layout: true});
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.responseTime());
app.use(express.errorHandler());
app.use(express.responseTime());
app.use(require('prerender-node'));

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

var routes = require('./routes')(app);

location: css, category: main.css 位置:css,类别:main.css

This and some of your errors are because your handlers for static content, css, and js are not set up to execute BEFORE app.router. 这和您的一些错误是因为未将静态内容,css和js的处理程序设置为在app.router之前执行。 Ordering of middleware in express must be carefully thought through and precise. 快速表达中间件的顺序必须经过仔细考虑和精确考虑。 So first get your middleware order correct so all your static CSs, JS, images, etc are handled before the app.router. 因此,首先要正确设置中间件顺序,以便在处理app.router之前处理所有静态CS,JS,图像等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM