简体   繁体   English

Angular2 Webpack和Express

[英]Angular2 Webpack and Express

I've installed Angular 2 Webpack which includes a sample app and demonstrates routing. 我已经安装了Angular 2 Webpack,其中包括一个示例应用程序并演示了路由。 What I want to look into is using Angular2 for the front end routing but to the use ExpressJS for a RESTful API backend, but on the same server ie 我想研究的是将Angular2用于前端路由,但将ExpressJS用于RESTful API后端,但在同一服务器上,即

http://localhost:3000/#/ will serve the front end in Angular http://localhost:3000/api will serve the back end API in Express http:// localhost:3000 /#/将在Angular中提供前端http:// localhost:3000 / api将在Express中提供后端API

If I create a api/index.html I can see this shows the api home page but I haven't got a clue how to create the 'app.js' for express to get the express routing working. 如果我创建api / index.html,我可以看到它显示了api主页,但是我不知道如何创建express的“ app.js”以使Express路由正常工作。

I appreciate I could easily do it by running two instances but I was trying to keep it all self contained. 我赞赏我可以通过运行两个实例轻松地做到这一点,但是我试图将其全部保持独立。

Regards 问候

I couldn't find a way to do it on the same port so I went with placing this in config/webpack.common.js. 我找不到在同一端口上执行此操作的方法,因此我将其放置在config / webpack.common.js中。 (I had thought I would need to start a separate 'npm' process for the API but clearly not!) (我以为我需要为该API启动单独的“ npm”过程,但显然不需要!)

 var express = require('express'); var app2 = express(); app2.get('/', function (req, res) { res.send('API'); }); app2.listen(3001, function () { }); 

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

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