【发布时间】:2018-09-25 20:57:37
【问题描述】:
以下是发布请求的代码:
export class AuthenticationService {
private authUrl = 'http://localhost:5555/api/auth';
constructor(private http: HttpClient) {}
login(username: string, password: string) {
console.log(username);
let data = {'username': username, 'password': password};
const headers = new HttpHeaders ({'Content-Type': 'application/json'});
//let options = new RequestOptions({headers: headers});
return this.http.post<any>(this.authUrl, JSON.stringify({data: data}), {headers: headers});
}
}
以下是我尝试访问请求正文的节点代码,在以下情况下,请求正文为空:
router.use(express.static(path.join('webpage')));
var bodyParser = require('body-parser');
router.use(bodyParser.urlencoded({ extended: true }));
router.post('/api/auth', function(req, res){
console.log(req.body);
console.log(req.body.username + ":" + req.body.password);
});
【问题讨论】:
-
你为什么要: 1. 自己在客户端将身体串起来;以及 2. 在服务器上使用 URL 编码的解析器?
-
这是你应该看的angular.io/guide/http
标签: node.js angular typescript http