简体   繁体   English

使用express.js服务聚合物应用程序路由的页面?

[英]Serving polymer app-routed pages with express.js?

I have a bunch of pages based off of the Polymer starter kit, which work fine with polymer serve . 我有一堆基于Polymer入门工具包的页面,这些页面可以很好地与polymer serve However, I need to send some ajax requests through a server for database querying, so I was forced to write my own server code with express.js. 但是,我需要通过服务器发送一些ajax请求以进行数据库查询,因此我不得不使用express.js编写自己的服务器代码。 My issue is that whenever I enter a page that isn't root and refresh, I get the message Cannot GET /bugs or whatever the /page name is. 我的问题是,每当我进入非root用户页面并刷新时,都会收到消息Cannot GET /bugs或/ page名称。 My index.html page references a container.html as normal, which has app-route code as follows: 我的index.html页面正常引用了container.html,其中包含应用程序路由代码,如下所示:

<app-location route="{{route}}"></app-location>
    <app-route
        route="{{route}}"
        pattern="/:page"
        data="{{routeData}}"
        tail="{{subroute}}"></app-route>

...

<app-drawer id="drawer">
        <app-toolbar>Menu</app-toolbar>
        <iron-selector selected="[[page]]" attr-for-selected="name" class="drawer-list" role="navigation">
          <a name="view-1" href="/view-1">View One</a>
          <a name="bugs" href="/bugs">Bugs</a>
        </iron-selector>
      </app-drawer>

...

<iron-pages
            selected="[[page]]"
            attr-for-selected="name"
            fallback-selection="view-404"
            role="main">
          <view-1 name="view-1"></view-1>
          <bugs-view name="bugs"></bugs-view>
        </iron-pages>

In other words, it's almost exactly the same as in the default Polymer starter kit. 换句话说,它与默认的Polymer入门工具包几乎完全相同。 My server code is as follows: 我的服务器代码如下:

var express = require('express');
var path = require('path');
var mysql = require('mysql');

var connection = mysql.createConnection({
    host     : 'host',
    user     : user,
    password : password,
    database : 'db'
});
connection.connect();
var app = express();

app.get('/sql', function(req,res) {
    //console.log(req.query.query);
    connection.query(req.query.query, function(error, results, fields) {
        if (error) {console.log(error); throw error;}
        else {
            res.send(results);
        }
    }.bind(this));
});

app.use(express.static(__dirname));
app.use(express.static(__dirname+'/src'));
app.listen(8080);

console.log("Server listening on localhost:8080");

I can load localhost:8080 fine, and navigate to pages just fine, but whenever I try to refresh on any page but root it gives me the Cannot GET /page error. 我可以很好地加载localhost:8080 ,并导航到页面也很好,但是每当我尝试刷新除根目录以外的任何页面时,它都会显示Cannot GET /page错误。 This has made development an intensely frustrating experience... 这使开发成为令人沮丧的体验。

Try adding this to your server config: 尝试将其添加到您的服务器配置中:

app.get('*', function(req, res) {
    res.sendfile('./public/index.html', {root: '.'}); 
});

Instead of ./public/index.html , put the path for your index.html 而不是./public/index.html ,而是放置index.html的路径

Put this AFTER all of your other routes so it does not override them.This way, no matter the page you reload on, your server will still send your "starting point", which will then of course pull in all of your javascript and css. 将其放在所有其他路由之后,这样就不会覆盖它们。这样,无论您重新加载的页面如何,您的服务器仍然会发送“起点”,然后当然会拉入所有的javascript和css 。 After all of that, once angular bootstraps, it's router will take over and take you where you should be. 毕竟,一旦进行了角引导,它的路由器就会接管并带您到应有的位置。

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

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