簡體   English   中英

無法獲得快遞中的二級路線

[英]Can't get second level routes working in express

抱歉,我是Node的新手。 我已經堅持了幾個小時。

server.js

app.use(express.static(__dirname + "/public"));

app.get('/', function(req, res){
  res.sendFile(path.resolve(templatesPath + 'index.html'));
});

app.get('*', function(req, res){
  res.sendFile(path.resolve(templatesPath + 'index.html'));
});

index.html是一個Angular應用程序。 我有使用Angular的HTML5路由(例如)正常工作的第一級路由。 http:// lh:3000 / staff ”或“ http:// lh:3000

但是,如果我添加其他級別或路由參數,例如“ http:// lh:3000 / staff / ”或“ http:// lh:3000 / staff / test ”,Express似乎會忽略express.static,而是使用get通配符把我所有的文件都變成index.html,這樣我的頁面就壞了。

感謝您的幫助

在輔助路由中,相對於輔助路由,它正在加載index.html中引用的資產。 我的臨時解決方案是添加:app.use('/ files /',express.static(path.join(__ dirname +“ / public”)))); 但我意識到現在最好更改解決方案。

staff/test ,靜態資產位於您的資產文件夾中嗎? 如果它們是靜態資產,則資產文件夾中的人員/測試路徑中必須有一個文件。

如果它們不是靜態資產,而是動態內容,請考慮使用express.router ,為staff制作路由器並將其添加為,

var staff = express.Router();
staff.use('/staff', staff)

//this is route handler for /staff/test
staff.post('/test', function(req, res, next){
  res.json(some-json-content)
})

嘗試這個:

app.get('*/*', function(req, res){
  res.sendFile(path.resolve(templatesPath + 'index.html'));
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM