【发布时间】:2018-02-21 16:37:06
【问题描述】:
我创建了 Angular 4 应用程序,我可以使用 ng serve --open 运行它,它在 localhost:4200 上运行,
我想要的是我还在同一个角度项目中使用nodejs 创建了 api,现在我想在localhost:4200/api 运行该 API,所以我尝试了这样的事情
我的 angular 4 和 nodejs 结构看起来像这样
/dist
/server
/routes
/server
/src
app.js
package.json
在我使用的 app.js 中
app.use(express.static(path.join(__dirname, 'dist')));
app.use('/app/api', apiRoutes);
const port = process.env.PORT || '3000';
server.listen(port, () => console.log(`API running on localhost:${port}`));
一旦我使用nodemon app.js 运行并转到localhost:3000,它就会运行我的 Angular 应用程序,它很好,而我在 localhost:3000/app/api 运行它也可以正常工作,
但是当我在 Angular 应用程序中进行更改时,它不会自动刷新我的应用程序,因为它当前正在运行节点应用程序以进行刷新,我需要运行 ng build 并且它会影响我对 Angular 应用程序的新更改
所以,我想要的是运行ng serve --open,它将运行 Angular 应用程序但不运行节点 js api,所以想要同时运行两者,一旦我从 Angular 应用程序或节点 js 应用程序中更改任何内容,它必须自动刷新。
【问题讨论】:
-
您希望在同一端口上为客户端和服务器提供服务,仅用于开发还是用于生产?
-
@Jarek 我想在同一端口上为客户端和服务器提供服务,仅用于开发和生产。
标签: node.js angular mean-stack