I use Node.js on the back end to bring all the fun and functions of JavaScript to my servers.
Unique indexes are useful in MongoDB for preventing duplicate data. Here's how I catch the duplicate key error so I can send a human-readable response to let users know what the problem is.
Written by Codemzy in Node.js on August 16th, 2024
Here are some ways you can pretty print your JSON responses to the browser with Express in Node.js - either globally or for a single route, using the `space` property of `JSON.stringify()`.
Written by Codemzy in Node.js on June 27th, 2024
In this blog post, we will create a function to upload a file to DigitalOcean Spaces using Node.js and the AWS SDK.
Written by Codemzy in Node.js on May 29th, 2024
In this blog post, we will create a function to move files to another folder or bucket in AWS S3. Because you can't move files in AWS SDK, we will copy the files to the new location and then delete the originals.
Written by Codemzy in Node.js on March 6th, 2024
In this blog post, we will create a function to delete a folder or directory in AWS S3. Because folders don't actually exist in S3, the function will get the objects at the prefix, and recursively delete them all.
Written by Codemzy in Node.js on March 5th, 2024
In this blog post, we will create a function to copy a folder or directory in AWS S3. We need to do a bit of extra work to copy objects because folders don't exist in S3 - even if it seems like they do!
Written by Codemzy in Node.js on February 9th, 2024
If your MongoDB data doesn't match up with the response object you need to provide, you can use `.project()` to change a field name and respond with a different field name instead!
Written by Codemzy in Node.js on January 24th, 2024
Here's something that caught me out this week - skip and limit order in MongoDB. The order of skip and limit isn't important in chaining (like `find` commands) but it is important in aggregation.
Written by Codemzy in Node.js on November 23rd, 2023
There's more than one way to update a document in MongoDB. Here's when (and why) I use `findOneAndUpdate` instead of `updateOne` to update a document with MongoDB in Node.js.
Written by Codemzy in Node.js on September 26th, 2023
When you use the `upsert` option with `findOneAndUpdate` in MongoDB, it's not easy to know if the document was inserted or updated. Here's how I check if a document was created when returning the document before, or after the update.
Written by Codemzy in Node.js on September 25th, 2023
If you want to automatically have your files expire (and get deleted) from DigitalOcean Spaces after a certain number of days, you can do it with s3 lifecycle rules. You can’t set this up in the web interface, so here's the code you need to create the rule using Node.js.
Written by Codemzy in Node.js on September 22nd, 2023
When you send a query string `req.query` back to Express, true and false booleans are converted into strings. Here's why, and how I parse them back to booleans - but only sometimes!
Written by Codemzy in Node.js on September 20th, 2023
Paginating your data in MongoDB can be done in a single query, and it's kind of simple. But with this next page trick, you can know when you've fetched the last page of data, and avoid an empty results page.
Written by Codemzy in Node.js on August 29th, 2023
When you compare `ObjectId()`'s with the strict equality operator `===`, you might be surprised to get false, even when they match. It's caught me out a few times, so let's see how we can compare the MongoDB `ObjectId()`.
Written by Codemzy in Node.js on July 21st, 2023
Here's how I set up a Node.js API rate limit in 59 lines of code using JavaScript and MongoDB. Each rate limit can be set with a custom time window and token count so you can decide the rules for your API calls.
Written by Codemzy in Node.js on July 12th, 2023
Are your Express route params `undefined`? If you are not getting your route parameter values where you need them, this blog post should help you out of `undefined` hell with three easy fixes!
Written by Codemzy in Node.js on June 30th, 2023
Choosing the best way to add versioning to my Node.js API became quite an adventure! In this blog post, I looked at three approaches and (finally) picked my favourite.
Written by Codemzy in Node.js on June 2nd, 2023
How should we structure folders and files in Node.js? I was unhappy and slightly confused by the MVC approach, and wanted to move to a more colocation/feature-based structure. Here's what I came up with.
Written by Codemzy in Node.js on November 28th, 2022
Image files can get big, so you might want to think about reducing image sizes when you let users upload photos on your website. Here's how you can use npm packages like sharp and multer to handle files and images in Node.js.
Written by Codemzy in Node.js on December 15th, 2021