Categories
JavaScript Answers

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

To get Node Express.js to 404 only on missing routes, we can return a 404 response with a middleware.

For instance, we write

router.use((req, res, next) => {
  next({
    status: 404,
    message: "Not Found",
  });
});

router.use((err, req, res, next) => {
  if (err.status === 404) {
    return res.status(400).render("404");
  }

  if (err.status === 500) {
    return res.status(500).render("500");
  }

  next();
});

at the bottom of our code to call next to send the 404 status to the last middleware.

Then we check the err.status for the value and call res.status to return the status we want.

And we call render to render the response body.

Categories
JavaScript Answers

How to add nested objects in Node Mongoose schemas?

To add nested objects in Node Mongoose schemas, we set the type property to another schema.

For instance, we write

const AddressSchema = mongoose.Schema({
  city: String,
  street: String,
  houseNumber: String,
});

const ContactInfoSchema = mongoose.Schema({
  tel: [Number],
  email: [String],
  address: {
    type: AddressSchema,
    required: true,
  },
});

const CustomerSchema = mongoose.Schema({
  firstName: String,
  lastName: String,
  company: String,
  connectInfo: ContactInfoSchema,
});

to create the CustomerSchema that has the connectInfo property set to the ContactInfoSchema.

And the ContactInfoSchema has the address property set to the AddressSchema as its type.

Categories
JavaScript Answers

How to populate a sub-document in Node Mongoose after creating it?

To populate a sub-document in Node Mongoose after creating it, we call the populate method.

For instance, we write

Item.find({})
  .populate("comments.created_by")
  .exec((err, items) => {
    console.log(items[0].comments[0].created_by.name);
  });

to call find to get the items from Items.

And we call populate to get the data for the comments.created_by field for each entry.

Then we call exec to return the results as a plain array.

We get the results from the items array.

Categories
JavaScript Answers

How to write to a CSV in Node.js?

To write to a CSV in Node.js, we call writeFile.

For instance, we write

let dataToWrite;
const fs = require("fs");

fs.writeFile("form-tracking/formList.csv", dataToWrite, "utf8", (err) => {
  if (err) {
    console.log("Some error occured");
  } else {
    console.log("It's saved!");
  }
});

to call writeFile to write the dataToWrite CSV string to the form-tracking/formList.csv file.

If there’re any errors then, err in the callback would be set.

Categories
JavaScript Answers

How to add wildcard mapping in entry of Node Webpack?

To add wildcard mapping in entry of Node Webpack, we use glob.

For instance, we write

const glob = require("glob");
// ...
module.exports = {
  //..
  entry: glob.sync("./src/scripts/*.js"),
};

to set entry to the pattern returned by glob.sync to add all the paths that match the pattern as entry points.