首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用Node.js实现HTML5mode中的角度布线

用Node.js实现HTML5mode中的角度布线
EN

Stack Overflow用户
提问于 2015-07-10 04:19:45
回答 2查看 10.2K关注 0票数 18

我知道对此还有其他答案,但他们似乎有一些警告。

This one causes a redirect,这对于我使用混合面板的前端应用程序来说是致命的,并且双倍加载混合面板将在浏览器中出现最大调用堆栈大小超过错误。

我个人不能使用的This one uses sendFile。为了诊断这个问题,我被建议使用express.static()

目前,我的代码如下所示:

home.js -一些路由,最后一条打算作为前端站点。

var indexPath = path.resolve( __dirname, '../' + process.env.PUBLIC_FOLDER )

router.get( '/:user/:stream/:slug', function( req, res, next ) {

    if ( req.headers['user-agent'].indexOf( 'facebook' ) != -1 ) {
        // stuff to handle the Facebook crawler
    } else return next()
})

router.get( '/*', function( req, res ) {
    express.static( indexPath )
})

server.js -配置节点/快速

app = express();

app 
    .use( morgan( 'dev' ) )
    .use(bodyParser.urlencoded( { limit: '50mb', extended: true } ) )
    .use( bodyParser.json( { limit: '50mb' } ) )
    .use( '/api', require('./routes/usersRoute.js') )
    .use( '/', require( './routes/home' ) )
    .on( 'error', function( error ){
       console.log( "Error: " + hostNames[i] + "\n" + error.message )
       console.log( error.stack )
    })

http
    .createServer( app ).listen( process.env.PORT )
    .on( 'error', function( error ){
       console.log( "Error: " + hostNames[i] + "\n" + error.message )
       console.log( error.stack )
    })

更多信息

您可以看到我正在尝试使用express.static()的原因是因为当我使用res.sendfile()时,我得到一个problem like this one,其中控制台显示为Unexpected token '<'。不幸的是,答案没有指定确切的修复方法,发问者也没有,他们说他们解决了问题,但没有分享答案。

在我的试验和错误中,我添加了更多要表达的内容,如下所示

.use( '/app/app.js', express.static( indexPath + '/app/app.js' ) )
.use( '/app/views', express.static( indexPath + '/app/views' ) )
.use( '/app/controllers', express.static( indexPath + '/app/views' ) )
.use( '/app/directives', express.static( indexPath + '/app/views' ) )
.use( '/app/vendor', express.static( indexPath + '/app/vendor' ) )
.use( '/js', express.static( indexPath + '/js' ) )
.use( '/css', express.static( indexPath + '/css' ) )
.use( '/fonts', express.static( indexPath + '/fonts' ) )
.use( '/images', express.static( indexPath + '/images' ) )
.use( '/api', require('./routes/usersRoute.js') )
.all( '/*', require( './routes/home' ) )

在我的home.js路由文件中添加了以下内容

router.get( '/*', function ( req, res ) {
    res.status( 200 ).set( { 'content-type': 'text/html; charset=utf-8' } )
    .sendfile( indexPath + '/index.html' )
})

在浏览器中,我可以看到我所有的文件都在加载,但上面显示了<错误。当我进行刷新时,我看到这个/*/路由被调用了数百次,所以我认为.use( '...', ... )配置被忽略了。

下面是Jonas请求的另一个示例。

var indexPath = path.resolve( __dirname, process.env.PUBLIC_FOLDER )

mongoose.connect( process.env.MONGOLAB_URI )

app = express();

app 
    .use( morgan( 'dev' ) )
    .use(bodyParser.urlencoded( { limit: '50mb', extended: true } ) )
    .use( bodyParser.json( { limit: '50mb' } ) )
    .use( '/api', require('./routes/usersRoute.js') )
    .use( '/', require( './routes/home.js' ) )
    .use( express.static( indexPath ) )
    .on( 'error', function( error ){
       console.log( "Error: " + hostNames[i] + "\n" + error.message )
       console.log( error.stack )
    })

我也在不使用.use( '/', require( './routes/home.js' ) )行的情况下执行了相同的操作,以尝试缩小任何问题的范围,但结果是相同的。如果我在URL中有#,页面就会加载,但是浏览器会删除# (到目前为止还不错)。但是如果我按刷新,或者手动输入URL,sans-hashbang,它会给出一个类似Cannot GET /home/splash的错误,其中/home/splash是我要转到的任何路径。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31327439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档