示例代码:
在组件中调用API并传入数据:
register() { const user = {username: this.username, password: this.password}; this.http.post('http://localhost:8000/api/users', user).subscribe(res => { console.log(res); }); }
后端API:
app.post('/api/users', function(req, res) { const user = new User({ username: req.body.username, password: req.body.password }); user.save(function(err, savedUser) { if (err) {return res.status(500).send();} res.send(savedUser); }); });