我正在使用 lite-server 运行 Angular 2 应用程序。当我尝试发出获取请求时,我收到外部链接的 404 错误。我可以成功获取目录中的文件。
Response {_body: Object, status: 404, ok: false, statusText: "Not Found", headers: Headers…}
_body:Object
headers:Headers
ok:false
status:404
statusText:"Not Found"
type:2
url:null
原型:身体
这是我的获取请求代码的组件:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { BreadcrumbService } from './breadcrumbService';
import 'rxjs/add/operator/map';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
@Component({
moduleId: module.id,
providers: [ BreadcrumbService],
templateUrl: 'html/appList.html',
styleUrls: ['css/list.css']
})
export class HealthComponent {
constructor(private http: Http) {
this.http = http;
this.http.get('https://jsonplaceholder.typicode.com/')
.map(response => response.json())
.subscribe(
function(response) { console.log(response)},
function(error) { console.log("Error happened " + error)},
function() { console.log("the subscription is completed")}
);
}
}
我试图配置我的 bs-config.js 文件,如下所示:
https://github.com/johnpapa/lite-server/issues/100
但我不确定这是否是我的问题的原因。这是那个文件:
var proxy = require('http-proxy-middleware');
var apiProxy = proxy('/api', {target:'http://jsonplaceholder.typicode.com/posts/1', changeOrigin: true});
module.exports = {
"port": 8000,
// "files": ["./src/**/*.{html,htm,css,js}"],
"server": {
middleware: {
1:apiProxy
},
"baseDir": [
// "./src",
"./"
]
}
};
任何帮助,将不胜感激!