Due to my starting level with React
and Node
/Express
and, above all, time restriction, I'm developing a single page application
without database
, so I have only static
pages.
I've configured react-router
to have clear url, using the hash
(http://localhost:8080/#/contacts
) i avoid also server calls:
let history = createHistory({
queryKey: false
});
I've the necessity to send mail
from my form
. To do this i've find nodemailer and i need to invoke it from an ajax
call.
Just to do some pratique with React i've configured my ajax call in my componentDidMount
function:
openForm() {
$('#contacts-form').on('submit', function(){
$.ajax({
url: '/mail',
type: 'post',
data: { msg: $('#data').val() },
success: function(data){
console.log(data);
},
error: function(){
console.log('err form');
}
});
return false;
});
}
componentDidMount() {
this.openForm();
}
Now, after I've installed body-parser
and set my form's action
to "/mail
", in my server.js
I've configured my .post
route
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({
extended: true
}));
app.post('/mail', function(req, res){
res.send('ajax call recived');
});
So now once I try to submit my form, my ajax call fails, in console i get this error:
POST http://localhost:8080/mail 404 (Not Found)
err form
It's the first time i'm working with these tools, where i'm doing wrong?