简体   繁体   English

通过Express在基本URL上提供Angular2应用

[英]Serving Angular2 app at a base URL with Express

I have an Angular2 app up and running and a very simple express server. 我有一个已运行的Angular2应用程序和一个非常简单的Express服务器。 Is it possible to only serve my application when a user visits '/app' (for example)? 是否可以仅在用户访问“ / app”时才提供我的应用程序? If yes how can it be implemented? 如果是,如何实施?

I intend to have multiple Angular apps on different URLs and all of these need scripts. 我打算在不同的URL上使用多个Angular应用,所有这些都需要脚本。 Hence a solution which takes care of which script to handle when would be desired. 因此,需要一种解决方案,该方案负责在需要时处理哪个脚本。


Edit- Along with the accepted answer, if using the angular-cli, the 'base-href' has to be set to the URL of the app when building the app. 编辑-与可接受的答案一起,如果使用angular-cli,则在构建应用程序时,必须将“ base-href”设置为应用程序的URL。

Yes, that's possible. 是的,那是可能的。

You have to define that endpoint as the only one: 您必须将该端点定义为唯一端点:

app.get('/app', function (req, res) {
  res.send(/* your application */)
})

You can use this: 您可以使用此:

// serve the angular web page
app.get('/', app.use(express.static(path.join(__dirname+'/../../public'))));

Note : change the dir name to reach your directory where contains the angular static files. 注意 :更改目录名称以进入包含角度静态文件的目录。

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

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