My Express API started as a simple project but has grown to 50+ routes. The codebase is getting messy. Currently everything is in one app.js file.
What's the recommended folder structure for a production-grade Express API? Should I be looking at something like NestJS at this scale?
Alex Martinez
asked 2 months ago1
Answers46
Upvotes4400
ViewsI'm running a Node.js app inside Docker and trying to connect to a MongoDB instance running on my host machine. The connection string mongodb://localhost:27017 fails inside the container.
Error: connect ECONNREFUSED 127.0.0.1:27017
I know localhost inside Docker refers to the container itself. How do I properly connect to the host's MongoDB?
I'm using .env files with dotenv but I'm not sure about best practices for managing different environments. My concerns:
.env.development, .env.staging, .env.production?I've been using async/await in my Node.js app but I keep running into unhandled promise rejections. Here's my current code:
async function fetchData() {
const res = await fetch('https://api.example.com/data');
const json = await res.json();
return json;
}
What's the correct pattern for catching errors? Should I use try/catch everywhere or is there a better approach?
Sofia Greco
asked 2 months ago1
Answers19
Upvotes6150
Views1
Answers38
Upvotes5501
Views2
Answers92
Upvotes4230
Views