简体   繁体   English

如何在Express for Node中重定向到单页Web应用程序

[英]How to Redirect to Single Page Web App in Express for Node

I am writing a website with a single page web app (the rest of the website is just static files which are served). 我正在编写一个带有单页Web应用程序的网站(该网站的其余部分只是提供的静态文件)。 I am trying to write a piece of middleware for express to redirect all requests that follow the pattern 'example.com/app' to 'example.com/app' so that requests such as 'example.com/app/my/specific/page/' will all result in the same page being sent. 我正在尝试编写一个用于Express的中间件,以将所有遵循模式'example.com/app'的请求重定向到'example.com/app',以便请求诸如'example.com/app/my/specific/ page /'将导致同一页面被发送。 The key issue with this is that the url in the address bar of the browser must not change so that the javascript app itself can interpret it and display the correct thing. 这个问题的关键问题是浏览器地址栏中的url不能更改,以便javascript应用程序本身可以解释它并显示正确的内容。

I could have done something like this: 我本可以这样做的:

app.use( '/app', function ( req, res ) {
    res.redirect('/app');
});

However, this causes the url of the page to change and a separate HTTP request is assumedly made. 但是,这会导致页面的URL更改,并且假定会发出单独的HTTP请求。

The most obvious alternative solution is to do something like this: 最明显的替代解决方案是做这样的事情:

app.use( '/app', function ( req, res ) {
    res.sendfile(__dirname + '/public/app/index.html');
});

The issue here is that resources from the page after requests like 'example.com/app/my/specific/page/' will look in the wrong location. 这里的问题是,像'example.com/app/my/specific/page/'这样的请求后,页面中的资源会显示在错误的位置。 For example, if I have an image on the page such as then it will look for example.com/app/my/specific/page/image.jpg. 例如,如果我在页面上有图像,那么它将查找example.com/app/my/specific/page/image.jpg。 Since no image is returned, it will not display on the page. 由于没有返回图像,因此不会在页面上显示。 This happens for all external scripts or stylesheets. 所有外部脚本或样式表都会发生这种情况。

I also tried something like this: 我也尝试过这样的事情:

app.use( '/app', function ( req, res ) {
    res.sendfile(__dirname + '/public/beta' + url.parse(req.url).pathname);
});

but that was very stupid of me for obvious reasons. 但由于显而易见的原因,这对我来说非常愚蠢。

In the end I used this middleware to serve the app's page when appropriate 最后,我使用此中间件在适当的时候提供应用程序的页面

// all unmatched requests to this path, with no file extension, redirect to the dash page
app.use('/dash', function ( req, res, next ) {
    // uri has a forward slash followed any number of any characters except full stops (up until the end of the string)
    if (/\/[^.]*$/.test(req.url)) {
        res.sendfile(__dirname + '/public/dash/index.html');
    } else {
        next();
    }
});

I then set used a base HTML element with the href attribute pointed to the root. 然后我设置使​​用base HTML元素,其中href属性指向根。

If you're still trying to accomplish this I may have found a starting point. 如果你还在努力实现这个目标,我可能已经找到了一个起点。 Alexander Beletsky has a Backbone.js + Express SPA boilerplate repo Located Here . 亚历山大Beletsky有Backbone.js的+快递SPA样板回购设在这里

For a brief article on how it came about you can read his article on Dzone . 有关它是如何形成的简短文章,您可以阅读他关于Dzone的文章。

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

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