如何使用 webpack 将 angular2 与现有的 spring 启动服务器相结合

How to use webpack combine angular2 with existing spring boot server

我想要现有的 spring 引导网关作为后端服务器,而不是 angular 2 。

that is i want the angular2 combines to the spring gateway .

假设:

the angular 2 webpack-dev-server on port 8090 , and there is a page index.html

spring boot gateway on port 8080: and all the api .

我要打开

localhost:8080 

看angular2index.html,

如何实施?

您需要为后端设置代理。我知道基于 Express 的 webpack-dev-server。所以,我可以告诉你如何使用 Express 运行 它:

var express = require('express');
var request = require('request');
var path = require('path');

var app = express();
app.use('/api', function(req, res) {
    var url = 'http://localhost:8080/api' + req.url;
    req.pipe(request(url)).pipe(res);
});

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

/* serves all the static files */
app.get(/^(.+)$/, function(req, res){
    res.sendFile( __dirname + req.params[0]);
});

app.listen(8090);

然后你通过 /api/entry-point 引用后端 API .

@Injectable()
export class HttpProductDiscountService {
    constructor(private _http: Http) {}

    getProductDiscounts() {
        return this._http.get('api/1.0/product-discount')
            .map(res => res.json())
    }
}

我找到了一个实现的例子,

前面是:https://github.com/springboot-angular2-tutorial/angular2-app

和spring-服务器是:https://github.com/springboot-angular2-tutorial/boot-app

参考这个例子,我已经成功搭建了一个简化 演示。