Categories
JavaScript Answers

How to send additional data on socket connection with JavaScript socket.io?

To send additional data on socket connection with JavaScript socket.io, we call the emit function with the data.

For instance, we write

socket.on("connect", () => {
  socket.emit("hello", data);
});

io.on("connection", (client) => {
  client.on("hello", (data) => {});
});

to call emit to emit the 'hello' event with the data we want to send to the client.

Categories
JavaScript Answers

How to run “npm start” with a specific browser with create-react-app?

To run "npm start" with a specific browser with create-react-app, we set the BROWSER environment variable.

For instance, in package.json, we write

{
  //...
  "scripts": {
    "start": "BROWSER='chrome' react-scripts start"
  }
  //...
}

to set the BROWSER environment variable to 'chrome'.

And then we run react-scripts start to start create-react-app with Chrome.

This works on windows.

On Linux, we write

{
  //...
  "scripts": {
    "start": "BROWSER='google-chrome-stable' react-scripts start"
  }
  //...
}

And on Mac OS, we write

{
  //...
  "scripts": {
    "start": "BROWSER='google chrome' react-scripts start"
  }
  //...
}
Categories
JavaScript Answers

How to set multiple file entry and output in project with webpack and JavaScript?

To set multiple file entry and output in project with webpack and JavaScript, we set the entry and output property.

For instance, in webpack.config.js, we write

module.exports = (env, options) => ({
  //...
  entry: {
    "dir1/js/bundle": path.resolve(__dirname, "/apps/dir1/js/main.js"),
    "dir2/foo": path.resolve(__dirname, "/apps/dir2/index.js"),
  },
  output: {
    path: path.resolve(__dirname, "/apps"),
    filename: "[name].js",
  },
  //...
});

to add the entry property and set it to an object with the paths of the entry point files.

And we set the output property to an object with the path to output the files by setting the path property.

We set filename to the output file name.

Categories
JavaScript Answers

How to generate timestamp unix epoch format with Node.js?

To generate timestamp unix epoch format with Node.js, we use the Date constructor.

For instance, we write

const ts = Math.floor(new Date().getTime() / 1000);

to create a new date with the current date and time with the Date constructor.

Then we call getTime to get its timestamp in milliseconds.

And then we divide that by 1000 to get the timestamp in seconds.

Finally, we call Math.floor with the timestamp in seconds to round it down to the nearest integer.

Categories
JavaScript Answers

How to chain multiple pieces of middleware for specific route in Node.js Express?

To chain multiple pieces of middleware for specific route in Node.js Express, we put the middlewares in an array.

For instance, we write

const middleware = {
  requireAuthentication: (req, res, next) => {
    console.log("private route list!");
    next();
  },
  logger: (req, res, next) => {
    console.log("Original request hit : " + req.originalUrl);
    next();
  },
};

app.get(
  "/",
  [middleware.requireAuthentication, middleware.logger],
  (req, res) => {
    res.send("Hello!");
  }
);

to add the middlewares into the middleware object.

Then we call app.get with an array with the middlewares as the 2nd argument to call them in the same order they’re listed before calling the route handler.