sarbikbetal / nodejs-task-queue Goto Github PK
View Code? Open in Web Editor NEWThis repo contains the sample code for the article "Simple Node.js task queue with bee-queue and redis"
This repo contains the sample code for the article "Simple Node.js task queue with bee-queue and redis"
Considering you are still an under-grad, a fair attempt.
there is some suggestions I have in API design.
An "Order" is a Noun. It represents a resource in your application.
GET, POST, PUT, DELETE are the HTTP methods.
These are verbs, and they impact what happens with the "Order".
POST "/order" can be used for creating an order.
GET "/order/1234" should be used to know the status of Order number 1234.
PUT "/order/1234" should be used to update/add items inside the Order number 1234.
DELETE "/order/1234" should be used to cancel the Order number 1234.
If you notice all the APIs have "/order/1234" in the URL. You should not need a verb like "status" in the URL.
GET "/orders" can be used for listing all order of that restaurant/
Query parameters are generally optional and are used on existing resource.
lets say if i read your API , "GET /status" , I will not able to make sense of it :).
a good example of query parameter is "GET /orders?item=biryani".
This means, fetch all the orders which have the item biryani in it :).
In a summary, HTTP method are verbs and hence URLS should have nouns (whenever possible)
Good start by you. Long way to go.
app.get("/order/:id", (req, res) => {
let orderId = req.params.id;
if (orderId) {
getStatus(orderId).then((result) => {
if (result.status == "succeeded"){
res.json({
progress: `Your order is ready ๐`,
order: result.order
})
}else{
res.json({
progress: `Your order is โฒ ${result.progress}% ready`,
order: result.order,
status: result.status
})
}
}).catch((err) => {
res.json(err);
})
} else
res.sendStatus(400)
})
to
app.get("/order/:id", (req, res) => {
let orderId = req.params.id;
if (!orderId) {
res.sendStatus(400);
return;
};
getStatus(orderId).then((result) => {
res.json({
progress: result.status == "succeeded" ?`Your order is ready ๐`: `Your order is โฒ ${result.progress}% ready`,
order: result.order,
status: result.status
})
}).catch((err) => {
res.sendStatus(500);
});
})
intention is, any wrong / incorrect data passed by client should be caught first;
status can be given out as "succeeded" to client. Its upto the client to use or not to use.
Return 500 error when server fails to process an API.
Hope this helps
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.