Code Monkey home page Code Monkey logo

nodejs-task-queue's People

Contributors

sarbikbetal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nodejs-task-queue's Issues

Improvement in API Design.

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.

Refactoring GET implementation

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.