In ionic I used service provider to access http.
Service.ts file is like this. data is a json object.
import { Injectable } from '@angular/core';
import { Http,Headers } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class Service {
constructor(public http: Http) {
console.log('Hello Service Provider');
}
addNewPlace(data:any){
let headers = new Headers();
headers.append('Content-Type', 'application/json');
console.log(data);
this.http.post("http://127.0.0.1:3000/postData/",data);
}
}
My node app.js file is like this
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.get('/',function(req,res){
res.send("Hello_World");
});
app.post('/postData',function(req,res){
console.log(">>>> "+req.body);
res.json('Thank You');
});
app.listen(3000,function(){
console.log('Server running at http://127.0.0.1:3000/');
});
Where is the place I wrong? I want to send data object to node server from service script and console it. But nothing happens with this code. And also no any errors!!!
My node server is running on port 3000.
My ionic program is running on port 8100.
via Sajitha Liyanage
No comments:
Post a Comment