Categories
JavaScript Answers

How to destroy cookie with Node.js?

To destroy cookie with Node.js, we call clearCookie.

For instance, we write

res.clearCookie("key");

to call clearCookie to clear the cookie with key 'key'.

Categories
JavaScript Answers

How to combine or merge JSON on Node.js?

To combine or merge JSON on Node.js, we use the spread operator.

For instance, we write

const o1 = { a: 1 };
const o2 = { b: 2 };
const obj = { ...o1, ...o2 };

to create the obj object by spreading the properties of o1 and o2 into a new object.

Categories
JavaScript Answers

How to export an async function in Node.js?

To export an async function in Node.js, we set the module.exports property.

For instance, we write

const doStuff = async () => {
  // ...
};

module.exports.doStuff = doStuff;

to define the doStuff function and set that as the value of the module.exports.doStuff to export it.

Categories
JavaScript Answers

How to create a basic static file server in Node.js?

To create a basic static file server in Node.js, we use Express.

For instance, we write

const express = require("express");
const app = express();
const port = process.env.PORT || 4000;

app.use(express.static(__dirname + "/public"));
app.listen(port);

to create a file server by calling express.static to expose the /public folder as a static folder.

We call app.listen to start a web server.

Categories
JavaScript Answers

How to use an include with attributes with Node Sequelize?

To use an include with attributes with Node Sequelize, we set the attribute property.

For instance, we write

Payment.findAll({
  where: {
    DairyId: req.query.dairyid,
  },
  attributes: {
    exclude: ["createdAt", "updatedAt"],
  },
  include: {
    model: Customer,
    attributes: ["customerName", "phoneNumber"],
  },
});

to call findAll to add the attributes property to exclude.

And we add include with the columns of Customer to include.