Currently the code looks very untidy, you should better add different layers to differentiate concerns.
What I personally prefer and recommend is to use a flow like this
Routes -> Controllers ->Services -> Repository
- Routes - Everything related to routing goes here
- Controllers - It only handles where does the request goes in order to get handled and what response does it get.
- Services - Implement business logic here
- Repository - Interaction with databases
If you need any assistance, contact me via mail anuragsinhadeveloper@gmail.com