My goal is simple : I want to send a post request from Angular and via http to my node.js
But, the request is not received.
Here is my Angular Code
const Data = [{
'name' : this.userForm.controls.name.value,
'email' : this.userForm.controls.email.value,
'Object': this.userForm.controls.Object.value,
'message': this.userForm.controls.message.value
}];
console.log('ready to send');
this.http.post('http://localhost:8012/sendmail', Data).subscribe();
I just format my data in Data to send it. It was part of all my try. The url is good, since I test with get method and it worked !
Now in Node.js
const express = require('express');
const path = require("path");
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
var db = mongoose.connect("mongodb://localhost:27017/test",
function( err, response){
if(err){console.log(err);}
});
var app = express();
app.use(bodyParser());
app.use(bodyParser.json({limit:'10mb'}));
app.use(bodyParser.urlencoded({extended:true}));
app.use(function(req,res,next){
res.setHeader('Access-Control-Allow-
Origin','http://localhost:4200');
res.setHeader('Access-Control-Allow-
Methods','http://localhost:4200','POST');
res.setHeader('Access-Control-Allow-
Credentials','http://localhost:4200', true);
next();
});
app.post("/sendmail",function (req, res, next){
console.log("wddddddf");
res.end('buck');
})
app.listen(8012, ()=>{
console.log('waf waf 8012 waf waf');
})
Here I just want to receive data and console.log to know it's working.
My problem is that the request doesn't seems able to reach my node.
Does someone could help me to understand why ?
Edit : It still doesn't work with the subscribe. I've this error:
