Categories
JavaScript Answers

How to include Node.js ES6 classes with require?

To include Node.js ES6 classes with require, we set module.exports to the class.

Then we can use require to import the class.

For instance, we write

class Animal {
  //...
}
module.exports = Animal;

to set module.exports to the Animal class to export the class in Animal.js.

Then we write

const Animal = require("./Animal");

class Dog extends Animal {
  //...
}

to require the Animal.js file to import the class.

And then we create the Dog class that inherits from the Animal class.

Categories
JavaScript Answers

How to suppress output when running npm scripts with JavaScript?

To suppress output when running npm scripts with JavaScript, we run npm run with the silent option.

For instance, we run

npm run --silent your-script

to run your-script without showing any output with the --silent option.

Categories
JavaScript Answers

How to add Node.js client for a socket.io server?

To add Node.js client for a socket.io server, we use the socket.io-client package.

For instance, on the Node.js client, we add

const io = require("socket.io-client");
const socket = io.connect("http://localhost:3000", { reconnect: true });

socket.on("connect", (socket) => {
  console.log("Connected!");
});
socket.emit("CH01", "me", "test msg");

to connect to localhost:3000 with io.connect.

We call on to listen for connection events.

And we call emit to emit the 'CH01' event with 'me' and 'test msg' as the content.

On server side, we write

const app = require("express")();
const http = require("http").Server(app);
const io = require("socket.io")(http);

io.on("connection", (socket) => {
  console.log("connection");

  socket.on("CH01", (from, msg) => {
    console.log("MSG", from, " saying ", msg);
  });
});

http.listen(3000, () => {
  console.log("listening on *:3000");
});

to listen for the 'CH01' event with on`.

We listen for the connection event with on.

And we get the values from emit on client side from the parameters.

Categories
JavaScript Answers

How to stop Node.js Express app that’s started with ‘npm start’?

To stop Node.js Express app that’s started with ‘npm start’, we use the pkill command.

For instance, in package.json, we add

{
  //...
  "scripts": {
    "start": "app.js",
    "stop": "pkill --signal SIGINT myApp"
  }
  //...
}

to add a stop script that runs pkill with the SIGINT signal to stop the myApp app.

We use myApp to kill app.js since we set process.title to 'myApp' in app.js.

Categories
JavaScript Answers

How to convert entity to plain object with JavaScript Sequelize?

To convert entity to plain object with JavaScript Sequelize, we set the raw option to true.

For instance, we write

db.Sensors.findAll({
  where: {
    nodeId,
  },
  raw: true,
  nest: true,
}).success((sensors) => {
  console.log(sensors);
});

to call findAll to return objects by calling it with the raw option set to true.

We get the returned results from the sensors parameter in the success callback.