【发布时间】:2015-09-28 09:59:07
【问题描述】:
我知道对此还有其他答案,但它们似乎有一些警告。
This one causes a redirect,这对于我使用 Mixpanel 的前端应用程序来说可能是致命的,并且双重加载 Mixpanel 会在浏览器中出现 Maximum Call Stack Size Exceeded 错误。
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 - 配置节点/express
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() 时,我遇到了问题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 是我要去的任何路径。
【问题讨论】:
-
如何为此添加赏金?已经4天了……
-
我不是 express 方面的专家,但我从来没有看到有人在关注UI-Router FAQ 中的示例时出错,这表明使用
app.all而不是router.get。跨度> -
您检查传递给
express.static的路径是否有效?indexPath的值是多少? -
简单地使 var app = express(); app.configure(function() { ... app.use(express.static(__dirname + '/public')); ... }
-
@PawełSmołka 我不认为 app.configure 是 express 4 的一部分。
标签: javascript angularjs node.js express angular-ui-router