我的http请求不会工作,角度2

时间:2017-04-30 03:20:00

标签: http angular2-routing

我正在使用MEAN堆栈

我已经成功地提供了一个简单的JSON对象{"消息":"你好"}发送带有快速get和res.send函数,如果我去了我的网页浏览器路线

同样在我的角度2前端我有一个http.request(),如果我放一个JSON测试api url,但是如果我将我的路由url放到我的简单JSON上然后我没有得到回复。知道原因可能是什么?

my angular 2 component.ts:

makeRequest(): void {
    this.loading = true;
    this.http.request('myurl/members')
    .subscribe((res: Response) => {
    this.data = res.json();
    this.loading = false;
    });
  }

我的server.js表示:

// Get dependencies
const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
var mongojs = require('mongojs');
var db = mongojs('sangha',['members']);
const app = express();

// Parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// Point static path to dist
app.use(express.static(path.join(__dirname, 'dist')));

// Set port
const port ='80';
app.set('port', port);

/**
 * Create HTTP server.
 */
const server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */
server.listen(port, () => console.log(`running on port ${port}`));


/*db methods
db.members.insert({"firstname":"Josh"});
db.members.findAndModify({
  query: {lastname: ''},
  update: {$set: {firstname: ''}},
  new: true
}, function (err, doc, lastErrorObject){
  //?
});
db.members.find(function(err,docs){
  console.log("members collection: "+docs;
});
*/

/*submit on login page, first check the username doesn't already exists, then 
db.members.insert*/
//app.post('/members',function(req,res)

app.get('/members',function(req,res){
  res.send({"message":"hello"});
});

//this catch all must come after all route definitions
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist/index.html'));
});

0 个答案:

没有答案