最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Rest API, where to put heavy computation in routes? - Stack Overflow

matteradmin4PV0评论

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
Add a ment  | 

1 Answer 1

Reset to default 9

Probably 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> }
Post a comment

comment list (0)

  1. No comments so far