admin管理员组文章数量:1026989
I am intending to design a Rest API, which receives user request and do heavy putation and return the result.
I am new to web programming. so just get familiar with basic API service.
what I know:
1) request sent by post method with JSON, because data is not so simple. 2) I followed some examples and been successful with basics.
so I think I better start writing the heavy putation part.
I want it as following.
1) receive post request, and start to pute
2) just after puting send "working msg" (I think I can just do re.send("MSG"))
my questions
1)but where should I put my heavy puting?
2)since I already responded with "MSG", how can I send another content when there is no request?
3) I have read about middleware, and feels like middleware handles things between receiving request and sending response. am I right?
It would be great if you could show me a simple examples.
I am intending to design a Rest API, which receives user request and do heavy putation and return the result.
I am new to web programming. so just get familiar with basic API service.
what I know:
1) request sent by post method with JSON, because data is not so simple. 2) I followed some examples and been successful with basics.
so I think I better start writing the heavy putation part.
I want it as following.
1) receive post request, and start to pute
2) just after puting send "working msg" (I think I can just do re.send("MSG"))
my questions
1)but where should I put my heavy puting?
2)since I already responded with "MSG", how can I send another content when there is no request?
3) I have read about middleware, and feels like middleware handles things between receiving request and sending response. am I right?
It would be great if you could show me a simple examples.
Share Improve this question asked Dec 15, 2016 at 16:48 arslanarslan 2,2348 gold badges37 silver badges63 bronze badges 5- REST is all about "resources" and what you're doing to them, it might help get better answers if you provided some context as to what the putation you're doing is. As a starting point though, HTTP 202 (Accepted) is a useful status code if you're just taking some input to process, but don't want to guarantee anything is going to work yet. – Tom Davies Commented Dec 15, 2016 at 17:01
- 1 Do it in background as this.lau said. Node.js is ONE THREAD ONLY, therefore if you do heavy processing in it, it blocks all the ining messages. – libik Commented Dec 15, 2016 at 17:07
- @TomDavies thx, I will look for how to do background jobs now – arslan Commented Dec 15, 2016 at 17:10
- @libik can you show me any example code of something like this? – arslan Commented Dec 15, 2016 at 17:11
- 2 @alim - example of what? If yuo google "node.js single thread" you find a lot information about it. – libik Commented Dec 15, 2016 at 17:23
1 Answer
Reset to default 9Probably the best way to implement this is to do the heavy processing server-side in the background, and provide a way for the client to check if the job is pleted or not.
For example, let's say you want to run some heavy calculation. You could create a resource like this:
POST /calculator
The client POST a calculation, then the resource queue the calculation job for later processing (maybe by some cron job on the server) and respond with a job resource:
{ "id": 123456, "status": "pending" }
Then the clients can check at any time if the job is pleted by checking the /jobs resource:
GET /jobs/123456
which initially might respond with this again:
{ "id": 123456, "status": "pending" }
Then when it's in progress:
{ "id": 123456, "status": "in_progress" }
And when it's done:
{ "id": 123456, "status": "done", "result": <some object that contains the result of the calculation> }
I am intending to design a Rest API, which receives user request and do heavy putation and return the result.
I am new to web programming. so just get familiar with basic API service.
what I know:
1) request sent by post method with JSON, because data is not so simple. 2) I followed some examples and been successful with basics.
so I think I better start writing the heavy putation part.
I want it as following.
1) receive post request, and start to pute
2) just after puting send "working msg" (I think I can just do re.send("MSG"))
my questions
1)but where should I put my heavy puting?
2)since I already responded with "MSG", how can I send another content when there is no request?
3) I have read about middleware, and feels like middleware handles things between receiving request and sending response. am I right?
It would be great if you could show me a simple examples.
I am intending to design a Rest API, which receives user request and do heavy putation and return the result.
I am new to web programming. so just get familiar with basic API service.
what I know:
1) request sent by post method with JSON, because data is not so simple. 2) I followed some examples and been successful with basics.
so I think I better start writing the heavy putation part.
I want it as following.
1) receive post request, and start to pute
2) just after puting send "working msg" (I think I can just do re.send("MSG"))
my questions
1)but where should I put my heavy puting?
2)since I already responded with "MSG", how can I send another content when there is no request?
3) I have read about middleware, and feels like middleware handles things between receiving request and sending response. am I right?
It would be great if you could show me a simple examples.
Share Improve this question asked Dec 15, 2016 at 16:48 arslanarslan 2,2348 gold badges37 silver badges63 bronze badges 5- REST is all about "resources" and what you're doing to them, it might help get better answers if you provided some context as to what the putation you're doing is. As a starting point though, HTTP 202 (Accepted) is a useful status code if you're just taking some input to process, but don't want to guarantee anything is going to work yet. – Tom Davies Commented Dec 15, 2016 at 17:01
- 1 Do it in background as this.lau said. Node.js is ONE THREAD ONLY, therefore if you do heavy processing in it, it blocks all the ining messages. – libik Commented Dec 15, 2016 at 17:07
- @TomDavies thx, I will look for how to do background jobs now – arslan Commented Dec 15, 2016 at 17:10
- @libik can you show me any example code of something like this? – arslan Commented Dec 15, 2016 at 17:11
- 2 @alim - example of what? If yuo google "node.js single thread" you find a lot information about it. – libik Commented Dec 15, 2016 at 17:23
1 Answer
Reset to default 9Probably the best way to implement this is to do the heavy processing server-side in the background, and provide a way for the client to check if the job is pleted or not.
For example, let's say you want to run some heavy calculation. You could create a resource like this:
POST /calculator
The client POST a calculation, then the resource queue the calculation job for later processing (maybe by some cron job on the server) and respond with a job resource:
{ "id": 123456, "status": "pending" }
Then the clients can check at any time if the job is pleted by checking the /jobs resource:
GET /jobs/123456
which initially might respond with this again:
{ "id": 123456, "status": "pending" }
Then when it's in progress:
{ "id": 123456, "status": "in_progress" }
And when it's done:
{ "id": 123456, "status": "done", "result": <some object that contains the result of the calculation> }
本文标签: javascriptRest APIwhere to put heavy computation in routesStack Overflow
版权声明:本文标题:javascript - Rest API, where to put heavy computation in routes? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745663393a2162028.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论