NodeJS/ExpressJS - Send response of http request back to client
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I created a wesite using Angular 2 and I am using nodejs as backend.
I managed to send a request from the angular client to the nodejs server and forwarded this from nodejs to another application via http request. My goal is now to send the response from the third application back to the angular client page (I managed also to get the response from the third application), but I am a newby in nodejs/express and I couldn't manage to get the response body out of the http request and send it back to the angular client.
In the nodejs server I have following code (mostly by looking into tutorials):
app.route('/api/test').post((req, res) => {
postCode(JSON.stringify(req.body));
//Here I want to send back the response from the third application
//to the Angular client (instead of 'Hi')
res.status(200).send('Hi');
});
function postCode(post_data) {
const post_options = {
host: '127.0.0.1',
port: '8080',
path: '/test',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(post_data)
}
};
// Set up the request
const post_req = http.request(post_options, function (res) {
res.setEncoding('utf8');
let responseString = ''
res.on('data', function (data) {
responseString += data;
console.log('Response data: ' + responseString);
});
res.on("end", function (data) {
responseString += data;
console.log('Response end: ' + responseString);
});
});
// post the data
post_req.write(post_data);
post_req.end();
}
So, how can I manage now to "use" the response of the http request and send it back in the app.route('/api/arq').post method to the angular client (Please see also first comment in code)?
Thank you very much and
Kind regards,
Yasemin
javascript node.js express
add a comment |
I created a wesite using Angular 2 and I am using nodejs as backend.
I managed to send a request from the angular client to the nodejs server and forwarded this from nodejs to another application via http request. My goal is now to send the response from the third application back to the angular client page (I managed also to get the response from the third application), but I am a newby in nodejs/express and I couldn't manage to get the response body out of the http request and send it back to the angular client.
In the nodejs server I have following code (mostly by looking into tutorials):
app.route('/api/test').post((req, res) => {
postCode(JSON.stringify(req.body));
//Here I want to send back the response from the third application
//to the Angular client (instead of 'Hi')
res.status(200).send('Hi');
});
function postCode(post_data) {
const post_options = {
host: '127.0.0.1',
port: '8080',
path: '/test',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(post_data)
}
};
// Set up the request
const post_req = http.request(post_options, function (res) {
res.setEncoding('utf8');
let responseString = ''
res.on('data', function (data) {
responseString += data;
console.log('Response data: ' + responseString);
});
res.on("end", function (data) {
responseString += data;
console.log('Response end: ' + responseString);
});
});
// post the data
post_req.write(post_data);
post_req.end();
}
So, how can I manage now to "use" the response of the http request and send it back in the app.route('/api/arq').post method to the angular client (Please see also first comment in code)?
Thank you very much and
Kind regards,
Yasemin
javascript node.js express
This seems like a pretty common pattern to me. Maybe this has been answered here already? stackoverflow.com/questions/39301227/…
– madflow
Jan 4 at 16:00
Do I have to put the pipe behind the http.request, because this doesn't work?
– Yasemin K.
Jan 4 at 16:14
add a comment |
I created a wesite using Angular 2 and I am using nodejs as backend.
I managed to send a request from the angular client to the nodejs server and forwarded this from nodejs to another application via http request. My goal is now to send the response from the third application back to the angular client page (I managed also to get the response from the third application), but I am a newby in nodejs/express and I couldn't manage to get the response body out of the http request and send it back to the angular client.
In the nodejs server I have following code (mostly by looking into tutorials):
app.route('/api/test').post((req, res) => {
postCode(JSON.stringify(req.body));
//Here I want to send back the response from the third application
//to the Angular client (instead of 'Hi')
res.status(200).send('Hi');
});
function postCode(post_data) {
const post_options = {
host: '127.0.0.1',
port: '8080',
path: '/test',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(post_data)
}
};
// Set up the request
const post_req = http.request(post_options, function (res) {
res.setEncoding('utf8');
let responseString = ''
res.on('data', function (data) {
responseString += data;
console.log('Response data: ' + responseString);
});
res.on("end", function (data) {
responseString += data;
console.log('Response end: ' + responseString);
});
});
// post the data
post_req.write(post_data);
post_req.end();
}
So, how can I manage now to "use" the response of the http request and send it back in the app.route('/api/arq').post method to the angular client (Please see also first comment in code)?
Thank you very much and
Kind regards,
Yasemin
javascript node.js express
I created a wesite using Angular 2 and I am using nodejs as backend.
I managed to send a request from the angular client to the nodejs server and forwarded this from nodejs to another application via http request. My goal is now to send the response from the third application back to the angular client page (I managed also to get the response from the third application), but I am a newby in nodejs/express and I couldn't manage to get the response body out of the http request and send it back to the angular client.
In the nodejs server I have following code (mostly by looking into tutorials):
app.route('/api/test').post((req, res) => {
postCode(JSON.stringify(req.body));
//Here I want to send back the response from the third application
//to the Angular client (instead of 'Hi')
res.status(200).send('Hi');
});
function postCode(post_data) {
const post_options = {
host: '127.0.0.1',
port: '8080',
path: '/test',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(post_data)
}
};
// Set up the request
const post_req = http.request(post_options, function (res) {
res.setEncoding('utf8');
let responseString = ''
res.on('data', function (data) {
responseString += data;
console.log('Response data: ' + responseString);
});
res.on("end", function (data) {
responseString += data;
console.log('Response end: ' + responseString);
});
});
// post the data
post_req.write(post_data);
post_req.end();
}
So, how can I manage now to "use" the response of the http request and send it back in the app.route('/api/arq').post method to the angular client (Please see also first comment in code)?
Thank you very much and
Kind regards,
Yasemin
javascript node.js express
javascript node.js express
asked Jan 4 at 15:46
Yasemin K.Yasemin K.
104
104
This seems like a pretty common pattern to me. Maybe this has been answered here already? stackoverflow.com/questions/39301227/…
– madflow
Jan 4 at 16:00
Do I have to put the pipe behind the http.request, because this doesn't work?
– Yasemin K.
Jan 4 at 16:14
add a comment |
This seems like a pretty common pattern to me. Maybe this has been answered here already? stackoverflow.com/questions/39301227/…
– madflow
Jan 4 at 16:00
Do I have to put the pipe behind the http.request, because this doesn't work?
– Yasemin K.
Jan 4 at 16:14
This seems like a pretty common pattern to me. Maybe this has been answered here already? stackoverflow.com/questions/39301227/…
– madflow
Jan 4 at 16:00
This seems like a pretty common pattern to me. Maybe this has been answered here already? stackoverflow.com/questions/39301227/…
– madflow
Jan 4 at 16:00
Do I have to put the pipe behind the http.request, because this doesn't work?
– Yasemin K.
Jan 4 at 16:14
Do I have to put the pipe behind the http.request, because this doesn't work?
– Yasemin K.
Jan 4 at 16:14
add a comment |
2 Answers
2
active
oldest
votes
EDIT: seems like this similar pattern was answered already - External API Calls With Express, Node.JS and Require Module thanks to @madflow for finding it.
First of all you're doing some extra work in my opinion, why not use a framework like express.js?
Anyhow - what I would do is send the request to your third party from within the /api/arq
endpoint, call the http.request
after receiving a request inside the route and inside res.on("end"...
pass the answer along.
I thought, that I am already using expressjs? How can I pass the answer in res.on("end")? I already tried res.send at this place but it didn't work.
– Yasemin K.
Jan 4 at 15:58
check my edited post above ☝🏻
– JBK
Jan 4 at 16:10
As already stated before, this doesn't work. I get the error message "Unresolved function or method status()"
– Yasemin K.
Jan 4 at 16:14
Please post your full code.
– JBK
Jan 4 at 16:19
1
Thx JBK. This was the solution.
– Yasemin K.
Jan 9 at 12:26
|
show 2 more comments
console.log('Response end: ' + responseString);
The above line is the point where you have the data you want to send in your response.
So send the response there, and not immediately after you call postCode
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042122%2fnodejs-expressjs-send-response-of-http-request-back-to-client%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
EDIT: seems like this similar pattern was answered already - External API Calls With Express, Node.JS and Require Module thanks to @madflow for finding it.
First of all you're doing some extra work in my opinion, why not use a framework like express.js?
Anyhow - what I would do is send the request to your third party from within the /api/arq
endpoint, call the http.request
after receiving a request inside the route and inside res.on("end"...
pass the answer along.
I thought, that I am already using expressjs? How can I pass the answer in res.on("end")? I already tried res.send at this place but it didn't work.
– Yasemin K.
Jan 4 at 15:58
check my edited post above ☝🏻
– JBK
Jan 4 at 16:10
As already stated before, this doesn't work. I get the error message "Unresolved function or method status()"
– Yasemin K.
Jan 4 at 16:14
Please post your full code.
– JBK
Jan 4 at 16:19
1
Thx JBK. This was the solution.
– Yasemin K.
Jan 9 at 12:26
|
show 2 more comments
EDIT: seems like this similar pattern was answered already - External API Calls With Express, Node.JS and Require Module thanks to @madflow for finding it.
First of all you're doing some extra work in my opinion, why not use a framework like express.js?
Anyhow - what I would do is send the request to your third party from within the /api/arq
endpoint, call the http.request
after receiving a request inside the route and inside res.on("end"...
pass the answer along.
I thought, that I am already using expressjs? How can I pass the answer in res.on("end")? I already tried res.send at this place but it didn't work.
– Yasemin K.
Jan 4 at 15:58
check my edited post above ☝🏻
– JBK
Jan 4 at 16:10
As already stated before, this doesn't work. I get the error message "Unresolved function or method status()"
– Yasemin K.
Jan 4 at 16:14
Please post your full code.
– JBK
Jan 4 at 16:19
1
Thx JBK. This was the solution.
– Yasemin K.
Jan 9 at 12:26
|
show 2 more comments
EDIT: seems like this similar pattern was answered already - External API Calls With Express, Node.JS and Require Module thanks to @madflow for finding it.
First of all you're doing some extra work in my opinion, why not use a framework like express.js?
Anyhow - what I would do is send the request to your third party from within the /api/arq
endpoint, call the http.request
after receiving a request inside the route and inside res.on("end"...
pass the answer along.
EDIT: seems like this similar pattern was answered already - External API Calls With Express, Node.JS and Require Module thanks to @madflow for finding it.
First of all you're doing some extra work in my opinion, why not use a framework like express.js?
Anyhow - what I would do is send the request to your third party from within the /api/arq
endpoint, call the http.request
after receiving a request inside the route and inside res.on("end"...
pass the answer along.
edited Jan 4 at 16:09
answered Jan 4 at 15:53
JBKJBK
514
514
I thought, that I am already using expressjs? How can I pass the answer in res.on("end")? I already tried res.send at this place but it didn't work.
– Yasemin K.
Jan 4 at 15:58
check my edited post above ☝🏻
– JBK
Jan 4 at 16:10
As already stated before, this doesn't work. I get the error message "Unresolved function or method status()"
– Yasemin K.
Jan 4 at 16:14
Please post your full code.
– JBK
Jan 4 at 16:19
1
Thx JBK. This was the solution.
– Yasemin K.
Jan 9 at 12:26
|
show 2 more comments
I thought, that I am already using expressjs? How can I pass the answer in res.on("end")? I already tried res.send at this place but it didn't work.
– Yasemin K.
Jan 4 at 15:58
check my edited post above ☝🏻
– JBK
Jan 4 at 16:10
As already stated before, this doesn't work. I get the error message "Unresolved function or method status()"
– Yasemin K.
Jan 4 at 16:14
Please post your full code.
– JBK
Jan 4 at 16:19
1
Thx JBK. This was the solution.
– Yasemin K.
Jan 9 at 12:26
I thought, that I am already using expressjs? How can I pass the answer in res.on("end")? I already tried res.send at this place but it didn't work.
– Yasemin K.
Jan 4 at 15:58
I thought, that I am already using expressjs? How can I pass the answer in res.on("end")? I already tried res.send at this place but it didn't work.
– Yasemin K.
Jan 4 at 15:58
check my edited post above ☝🏻
– JBK
Jan 4 at 16:10
check my edited post above ☝🏻
– JBK
Jan 4 at 16:10
As already stated before, this doesn't work. I get the error message "Unresolved function or method status()"
– Yasemin K.
Jan 4 at 16:14
As already stated before, this doesn't work. I get the error message "Unresolved function or method status()"
– Yasemin K.
Jan 4 at 16:14
Please post your full code.
– JBK
Jan 4 at 16:19
Please post your full code.
– JBK
Jan 4 at 16:19
1
1
Thx JBK. This was the solution.
– Yasemin K.
Jan 9 at 12:26
Thx JBK. This was the solution.
– Yasemin K.
Jan 9 at 12:26
|
show 2 more comments
console.log('Response end: ' + responseString);
The above line is the point where you have the data you want to send in your response.
So send the response there, and not immediately after you call postCode
.
add a comment |
console.log('Response end: ' + responseString);
The above line is the point where you have the data you want to send in your response.
So send the response there, and not immediately after you call postCode
.
add a comment |
console.log('Response end: ' + responseString);
The above line is the point where you have the data you want to send in your response.
So send the response there, and not immediately after you call postCode
.
console.log('Response end: ' + responseString);
The above line is the point where you have the data you want to send in your response.
So send the response there, and not immediately after you call postCode
.
answered Jan 4 at 16:21
community wiki
Quentin
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042122%2fnodejs-expressjs-send-response-of-http-request-back-to-client%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This seems like a pretty common pattern to me. Maybe this has been answered here already? stackoverflow.com/questions/39301227/…
– madflow
Jan 4 at 16:00
Do I have to put the pipe behind the http.request, because this doesn't work?
– Yasemin K.
Jan 4 at 16:14