Skip to content

Commit

Permalink
remove default status calls
Browse files Browse the repository at this point in the history
  • Loading branch information
fcasibu committed Apr 2, 2024
1 parent 712e10d commit 776aaea
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions nodeJS/express/controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ There is also a useful method that you can use to set the status code manually.

If `res.send` automatically sets the `Content-Type` based on the data passed, why would we still use `res.json`? `res.json` enforces JSON and will automatically convert non-object values to JSON, but `res.send` will not. `res.json` is just a convenient method that also internally calls `res.send`. `res.send` will only handle things as JSON when dealing with booleans and objects (which includes arrays).

So for convenience it's more appropriate to use `res.json` instead of `res.send`, and if you are sending JSON then you might as well go for a method that is *named "json"* :)
So for convenience it's more appropriate to use `res.json` instead of `res.send`, and if you're sending JSON, you might as well use a method that's literally named "json"! It's like the perfect match!

</div>

Expand Down Expand Up @@ -130,7 +130,7 @@ const getUserById = async (req, res) => {
return;
}

res.status(200).send(`User found: ${user.name}`);
res.send(`User found: ${user.name}`);
};
```

Expand Down Expand Up @@ -165,13 +165,13 @@ const getUserById = async (req, res) => {
return;
}

res.status(200).send(`User found: ${user.name}`);
res.send(`User found: ${user.name}`);
} catch (error) {
console.error("Error retrieving user:", error);
res.status(500).send("Internal Server Error");

// or we can call next(error) instead of sending a response here
// Using `next(error)` however will only render an error page in the express' default view.
// Using `next(error)` however will only render an error page in the express' default view and respond with the whole html to the client.
// So we will need to create a special type of middleware if we want a different response and we will get to that in a bit.
}
};
Expand All @@ -193,7 +193,7 @@ const getUserById = asyncHandler(async (req, res) => {
return;
}

res.status(200).send(`User found: ${user.name}`);
res.send(`User found: ${user.name}`);
});
```

Expand Down Expand Up @@ -244,7 +244,8 @@ class CustomNotFoundError extends Error {
constructor(message) {
super(message);
this.statusCode = 404;
this.name = "NotFoundError"; // So the error is neat when stringified. NotFoundError: message instead of Error: message
// So the error is neat when stringified. NotFoundError: message instead of Error: message
this.name = "NotFoundError";
}
}
```
Expand All @@ -261,7 +262,7 @@ const getUserById = asyncHandler(async (req, res) => {
throw new CustomNotFoundError("User not found");
}

res.status(200).send(`User found: ${user.name}`);
res.send(`User found: ${user.name}`);
});
```

Expand Down Expand Up @@ -346,7 +347,7 @@ const getUserById = asyncHandler(async (req, res) => {
throw new CustomNotFoundError("User not found");
}

res.status(200).send(`User found: ${user.name}`);
res.send(`User found: ${user.name}`);
});

const getUsers = asyncHandler(async (req, res) => {
Expand Down Expand Up @@ -421,7 +422,8 @@ app.use(express.urlencoded({ extended: false }));

app.use((req, res, next) => {
// You can of course also create your own for your own use-case!
next(); // Just make sure to call `next`
// Just make sure to call `next`
next();
})

// base mount path is `/users` and will always execute on that specific mount path, and yes including `/users/a/b/c`
Expand Down

0 comments on commit 776aaea

Please sign in to comment.