Home  >  Q&A  >  body text

node.js - Solve the multi-layer nested relationship in nodejs

My project is an e-commerce website, and the backend is implemented using node. All the code is written in the same js file, which is very inconvenient. The first is the code to connect to the database. In the callback function to connect to the database, define Schema and model. The route is then processed and the template is rendered in the route's callback function. Who can solve this multi-level nesting problem?

PHP中文网PHP中文网2698 days ago623

reply all(4)I'll reply

  • 黄舟

    黄舟2017-05-31 10:41:48

    Regarding the callback problem, several other students have said that you can use promise, async/await, etc. to solve the callback curse.

    As for all codes being placed in the same js file, if you don’t have back-end development experience, you can easily write the back-end directly using node. Express, which is currently the most widely used, is relatively simple compared to traditional web development frameworks, and koa is even simpler.

    They only introduce how to obtain requests, return requests, etc. in the document, but they will not introduce you to how to structure large applications. If a novice does not have layering awareness, the most he can do is write a controller and then throw all the business logic into it.

    As far as I know so far, you can try eggjs. At least the document helps you list the levels of service, controller, and model. It may be helpful for your development.

    reply
    0
  • 黄舟

    黄舟2017-05-31 10:41:48

    In addition to the asynchronous issues mentioned by everyone above (solved by using promises, async/await, etc.), I think you should pay attention to the issue of modularization.

    You put all the code into one file, which is annoying enough even without these multiple levels of nesting.

    On the contrary, if the modularization is done better, you may not feel uncomfortable with these nesting problems.

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-31 10:41:48

    ..prommise,generator,async/await you all mean to have it. . . Converting asynchronous to synchronous writing is no longer a dream! ~! ~!

    reply
    0
  • PHP中文网

    PHP中文网2017-05-31 10:41:48

    It’s similar to my graduation project. I also hung it on node.js. I did the same thing at the beginning. Then I studied promises and then used this one. I thought it was pretty good. You can also give it a try.

    reply
    0
  • Cancelreply