DEV Community

Discussion on: Node.js, MongoDB, and Express Rest API (Part 1)

Collapse
 
bias profile image
Tobias Nickel

how do you like using async/await functions? then you can avoid lots of the callbacks.

Collapse
 
eidorianavi profile image
EidorianAvi

Are you talking about with the get and post requests? I'd love to see an example

Collapse
 
eidorianavi profile image
EidorianAvi • Edited

Something like this?

router.post('/add-dog', async (req, res) => {
    const dog = new Dog({
        name: req.body.name,
        breed: req.body.breed,
    });

   try {
      const saved = await dog.save();
      res.json(saved)
   } catch(e){
     res.json(e);
   }
});
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bias profile image
Tobias Nickel

yes, and did you know, with the newer framework 'fastify' you can leave out the try catch block. and get some more advantages. while all the express middleware you are useing will still work.

Thread Thread
 
eidorianavi profile image
EidorianAvi

I've never heard of fastify. Sounds promising though. I'll be looking into that.

Thread Thread
 
bias profile image
Tobias Nickel

By the way, in my opinion there is nothing wrong with 'express' you can certainly build complete projects with it. My personal Website is still running on express.

But since IBM accuired the developers, express stopt progressing. That is also a good thing, because it is very stable.

backed by the OpenJS foundation, 'fastify' is also very stable.

I am giving lots of tips here, but I want to thank you for sharing this tutorial and hope you share more.

Thread Thread
 
eidorianavi profile image
EidorianAvi

I welcome any and all constructive conversation on coding! I'm always open to new ideas and have plenty to learn myself so I appreciate you checking out my post and also your knowledgeable input. Thanks Tobias.