I'm using node.js, express, request-promise, and I'm writing a server.js
file. In the .then
statement, I have res.sendFile(__dirname + '/public/thanks.html');
and it seems not to be doing... well anything. When I inspect using Chrome's dev tools and looking at the network tab, I never receive anything called thanks.html
on the client at all. There isn't a problem with rp(slackPost)
because I properly receive the message in Slack. console.log('Worked!');
and console.log('sent');
both work perfectly fine. Am I using the res.sendFile()
function incorrectly?
Here's the relevant section of my server.js
file:
app.post('/contact_us', function (req, res) {
//send input to Slack
// console.log('Working.');
// console.log(req.body);
//sets options to create a POST request (request-promise) to send the stuff to slack
var slackPost = {
headers: {
'Content-type': 'application/json'
},
method: 'POST',
uri: 'https://hooks.slack.com/services/API/key/here',
body: '{\x22text\x22:\x22' + req.body.fname + '\n' + req.body.email + '\n' + req.body.info + '\x22}',
json: false // Automatically stringifies the body to JSON
};
// console.log('Still working.');
rp(slackPost)
.then(function (parsedBody) {
// POST succeeded...
console.log('Worked!');
res.sendFile(__dirname + '/public/thanks.html');
// res.redirect('public/thanks.html');
// res.json(null);
console.log('sent');
})
.catch(function (err) {
// POST failed...
// console.log('Failed :(');
console.log(err);
});
});
And here's the script that runs on submit:
function sendToSlack() {
var fname = document.forms["contact"]["fname"].value;
var email = document.forms["contact"]["email"].value;
var info = document.forms["contact"]["info"].value;
var formInput = {
fname: fname,
email: email,
info: info
};
fetch('/contact_us', {
method: 'POST',
headers: {
'Content-Type':'application/json'
},
body: JSON.stringify(formInput)
})
.then(function(res) {
window.location.replace('public/thanks.html');
console.log('Posted');
} )
.catch(function(err) {
console.log('problem');
} );
}