【发布时间】:2019-01-30 00:13:40
【问题描述】:
我是这个领域的新手。我从本地的Node+Typescript 示例开始。听代码:
import express from 'express';
const app = express();
var counter: number=0;
app.get('/', (req, res) => {res.send('Hello World! ${counter}' + counter.toString()); counter++;} );
app.listen(3000, () => console.log('Example app listening on port 3000!'));
我还为客户端编写了一些 Angular 代码:
@Component({
selector: 'test',
template: '<div>Simple Http: {{counter}}</div><button (click)="onClickMe()">Click me!</button>'
})
export class test {
counter: any="Not yet";
constructor(private http: HttpClient) { }
onClickMe() {
this.counter = 'Try read from server..';
this.http.get("http://localhost:3000/").subscribe(val=>this.counter=val);
}
}
我通过 npm 运行客户端:“ng serve”http://localhost:4200/
我收到错误:
从源访问“http://localhost:3000/”处的 XMLHttpRequest 'http://localhost:4200' 已被 CORS 策略阻止:否 请求中存在“Access-Control-Allow-Origin”标头 资源。
我知道有一个名为“cors”的库。但我不确定如何使用 Typescript 安装它或如何使用它。
【问题讨论】:
标签: node.js typescript