I have a route in my express.js app on a domain example1
:
router.post('/messages/add', (req, res) => {
let message = new Message();
message.title = req.body.title;
message.body = req.body.body;
message.save(err => {
if(err) {
return res.json({ success: false });
} else {
return res.json({ success: false });
}
});
});
How do I pass the message to another express app's endpoint on a domain example2
?
UPDATE
Updated the code using the request module suggested by YouneL. example2
receives an empty object:
UPDATE 2
Finally got it working. Had to put form:formData
instead of formData:formData
in the request.post()
options.
Server/domain 1:
router.post('/messages/add', function (req, res) {
let formData = {
title: req.body.title,
body: req.body.body
}
request.post({
url:'',
form:formData}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
});
Server/domain 2:
router.post('/contacts/add', function (req, res) {
let message = new Message(); // Mongoose model
message.title = req.body.title;
message.body = req.body.body;
req.checkBody('title', 'Title is required').notEmpty();
req.checkBody('body', 'Body is required').notEmpty();
let errors = req.validationErrors();
if (errors) {
res.json({ success: false, msg: errors });
} else {
contact.save(function (err) {
if (err) {
console.log(err);
res.json({ success: false, msg: 'Failed to add message' });
} else {
res.json({ success: true, msg: 'Message added' });
}
});
}
});
I have a route in my express.js app on a domain example1.
:
router.post('/messages/add', (req, res) => {
let message = new Message();
message.title = req.body.title;
message.body = req.body.body;
message.save(err => {
if(err) {
return res.json({ success: false });
} else {
return res.json({ success: false });
}
});
});
How do I pass the message to another express app's endpoint on a domain example2.
?
UPDATE
Updated the code using the request module suggested by YouneL. example2.
receives an empty object:
UPDATE 2
Finally got it working. Had to put form:formData
instead of formData:formData
in the request.post()
options.
Server/domain 1:
router.post('/messages/add', function (req, res) {
let formData = {
title: req.body.title,
body: req.body.body
}
request.post({
url:'http://example2./api/messages/add',
form:formData}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
});
Server/domain 2:
router.post('/contacts/add', function (req, res) {
let message = new Message(); // Mongoose model
message.title = req.body.title;
message.body = req.body.body;
req.checkBody('title', 'Title is required').notEmpty();
req.checkBody('body', 'Body is required').notEmpty();
let errors = req.validationErrors();
if (errors) {
res.json({ success: false, msg: errors });
} else {
contact.save(function (err) {
if (err) {
console.log(err);
res.json({ success: false, msg: 'Failed to add message' });
} else {
res.json({ success: true, msg: 'Message added' });
}
});
}
});
Share
Improve this question
edited Feb 4, 2018 at 21:21
mikebrsv
asked Feb 4, 2018 at 1:17
mikebrsvmikebrsv
1,9702 gold badges26 silver badges32 bronze badges
5
- Seems like a duplicate to stackoverflow./questions/11355366/… – SteveB Commented Feb 4, 2018 at 1:25
- No, it's not. My question is specific for the express.js package. – mikebrsv Commented Feb 4, 2018 at 1:30
- It would work in express but if you want express specific, stackoverflow./questions/28352871/… – SteveB Commented Feb 4, 2018 at 1:32
- I appreciate your inputs, but I don't need to redirect a user to another URL. What I need is to make a post request to another domain. – mikebrsv Commented Feb 4, 2018 at 1:38
- So you want to take the request and call another endpoint and wait for the response like a proxy? Given the second domain, I was assuming a redirect would work. – SteveB Commented Feb 4, 2018 at 1:40
1 Answer
Reset to default 4You could use request module to send another post request to example2.
, here is an example:
router.post('/messages/add', (req, res) => {
let message = new Message();
message.title = req.body.title;
message.body = req.body.body;
message.save(err => {
if (err) {
return res.json({ success: false });
}
// post data to example2.
request.post({ url:'example2.', form: req.body }, (err, httpResponse, body) => {
if (err) {
return res.json({ success: false, msg: 'cannot post to example2.' });
}
res.json({ success: true });
});
});
});