Categories
JavaScript Answers

How to get Express.js to send 404 only on missing routes?

Sometimes, we want to get Express.js to send 404 only on missing routes.

In this article, we’ll look at how to get Express.js to send 404 only on missing routes.

How to get Express.js to send 404 only on missing routes?

To get Express.js to send 404 only on missing routes, we can add a catch-all route at the end of the app.

For instance, we write

const express = require("express");
const app = express.createServer();

const users = [{ name: "abc" }];

app.all("/user/:id/:op?", (req, res, next) => {
  req.user = users[req.params.id];
  if (req.user) {
    next();
  } else {
    next(new Error("cannot find user ", req.params.id));
  }
});

app.get("/user/:id", (req, res) => {
  res.send("viewing " + req.user.name);
});

app.get("/user/:id/edit", (req, res) => {
  res.send("editing " + req.user.name);
});

app.put("/user/:id", (req, res) => {
  res.send("updating " + req.user.name);
});

app.get("*", (req, res) => {
  res.send("not found", 404);
});

app.listen(3000);

to add

app.get("*", (req, res) => {
  res.send("not found", 404);
});

at the end of the app to add a catch all route with a route handler that only runs when no other URLs listened above it is matched.

We call res.send to send a 404 response.

Conclusion

To get Express.js to send 404 only on missing routes, we can add a catch-all route at the end of the app.

Categories
JavaScript Answers

How to set default path (route prefix) in Express?

Sometimes, we want to set default path (route prefix) in Express.

In this article, we’ll look at how to set default path (route prefix) in Express.

How to set default path (route prefix) in Express?

To set default path (route prefix) in Express, we can create a Router instance.

For instance, we write

const router = express.Router();
router.use("/user", user);

app.use("/api/v1", router);

to call express.Router to create a router.

Then we call router.use with a user route object.

Next, we call app.use with router to use the router in the app.

Therefore, we access the routes in the user router by prepending /api/v1/user to the URL.

Conclusion

To set default path (route prefix) in Express, we can create a Router instance.

Categories
JavaScript Answers

How to get the whole response body when the response is chunked in Node.js?

Sometimes, we want to get the whole response body when the response is chunked in Node.js.

In this article, we’ll look at how to get the whole response body when the response is chunked in Node.js.

How to get the whole response body when the response is chunked in Node.js?

To get the whole response body when the response is chunked in Node.js, we an listen to the on and end events.

For instance, we write

request.on("response", (response) => {
  let body = "";
  response.on("data", (chunk) => {
    body += chunk;
  });
  response.on("end", () => {
    console.log(body);
  });
});
request.end();

to call request.on with 'response' to listen for the response.

Then we call response.on with 'data' to listen for the response chunks and append them to body.

Next, we listen to the end event with a callback that runs when we got the whole response, so we log the full response body in the callback.

Conclusion

To get the whole response body when the response is chunked in Node.js, we an listen to the on and end events.

Categories
JavaScript Answers

How to fix username and password not accepted when using Nodemailer?

Sometimes, we want to fix username and password not accepted when using Nodemailer.

In this article, we’ll look at how to fix username and password not accepted when using Nodemailer.

How to fix username and password not accepted when using Nodemailer?

To fix username and password not accepted when using Nodemailer, we should try to allow less secure apps to access account.

We can allow less secure apps to access a Gmail account by going to https://myaccount.google.com/lesssecureapps and then toggle the option on.

Then we can log in and and send an email message with

const nodemailer = require("nodemailer");

const transporter = nodemailer.createTransport({
  host: "smtp.gmail.com",
  port: 587,
  secure: false,
  requireTLS: true,
  auth: {
    user: "your.gmail.account@gmail.com",
    pass: "your.password",
  },
});

const mailOptions = {
  from: "your.gmail.account@gmail.com",
  to: "receivers.email@domain.com",
  subject: "Test",
  text: "Hello World!",
};

transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    return console.log(error.message);
  }
  console.log("success");
});

We create a nodemailer.createTransport instance with the credentials and SMTP server host address.

And then we call sendMail with the mailOptions object that has the values for the email message to send the email.

Conclusion

To fix username and password not accepted when using Nodemailer, we should try to allow less secure apps to access account.

Categories
JavaScript Answers

How to broadcast messages on a namespace with socket.io and Node.js?

Sometimes, we want to broadcast messages on a namespace with socket.io and Node.js.

In this article, we’ll look at how to broadcast messages on a namespace with socket.io and Node.js.

How to broadcast messages on a namespace with socket.io and Node.js?

To broadcast messages on a namespace with socket.io and Node.js, we can use the of method.

For instance, we write

const io = require("socket.io").listen(80);
io.of("/admins").emit("message", { message: "Hello admins!" });

to call io.of with the namespace to broadcast to.

And then we call emit to send the message to that namespace.

Conclusion

To broadcast messages on a namespace with socket.io and Node.js, we can use the of method.