Categories
JavaScript Answers

How to use socket.io in Express 4, express-generator’s /bin/www, and JavaScript?

Sometimes, we want to use socket.io in Express 4 and express-generator’s /bin/www.

In this article, we’ll look at how to use socket.io in Express 4 and express-generator’s /bin/www.

How to use socket.io in Express 4 and express-generator’s /bin/www?

To use socket.io in Express 4 and express-generator’s /bin/www, we can add the socket.io initialization code in its own file.

For instance, in socketApi.js, we write

const io = require("socket.io")();
const socketApi = {
  io,
};

io.on("connection", (socket) => {
  console.log("A user connected");
});

module.exports = socketApi;

to call io.on with 'connection' to listen for the connection event.

We export the socketApi with

module.exports = socketApi;

Then in /bin/www, we incorporate the socket.io code with

const app = require("../app");
const debug = require("debug")("socketexpress:server");
const http = require("http");
const socketApi = require("../socketApi");

const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);

const server = http.createServer(app);
socketApi.io.attach(server);

//...

We call require to require the socketApi.js file.

Then we call socketApi.io.attach with the Express server to run the socket.io initialization code when we run our Express server.

Conclusion

To use socket.io in Express 4 and express-generator’s /bin/www, we can add the socket.io initialization code in its own file.

Categories
JavaScript Answers

How to convert string into array of integers with JavaScript?

Sometimes, we want to convert string into array of integers with JavaScript.

In this article, we’ll look at how to convert string into array of integers with JavaScript.

How to convert string into array of integers with JavaScript?

To convert string into array of integers with JavaScript, we can use the string split and array map methods.

For instance, we write

const arr = "14 2".split(" ").map(Number);

to call split with ' ' to split the string by the spaces into an array of substrings.

Then we call map with Number to convert each substring into a number and return a new array with the numbers.

Conclusion

To convert string into array of integers with JavaScript, we can use the string split and array map methods.

Categories
JavaScript Answers

How to use form input to access camera and immediately upload photos using web app with JavaScript?

Sometimes, we want to use form input to access camera and immediately upload photos using web app with JavaScript.

In this article, we’ll look at how to use form input to access camera and immediately upload photos using web app with JavaScript.

How to use form input to access camera and immediately upload photos using web app with JavaScript?

To use form input to access camera and immediately upload photos using web app with JavaScript, we can set the accept attribute of the file input to image/*;capture=camera.

For instance, we write

<input id="myFileInput" type="file" accept="image/*;capture=camera" />

to add a file input that accepts photos captured from a camera.

Then we write

const myInput = document.getElementById("myFileInput");

const sendPic = () => {
  const file = myInput.files[0];
  //...
};

myInput.addEventListener("change", sendPic, false);

to select the file input with getElementById.

Then we call addEventListener to listen to the change event triggered by the file input.

We call sendPic to get the image from myInput.files[0].

And then we can do what we want with it.

Conclusion

To use form input to access camera and immediately upload photos using web app with JavaScript, we can set the accept attribute of the file input to image/*;capture=camera.

Categories
JavaScript Answers

How to get data out of a Node.js HTTP get request with JavaScript?

Sometimes, we want to get data out of a Node.js HTTP get request with JavaScript.

In this article, we’ll look at how to get data out of a Node.js HTTP get request with JavaScript.

How to get data out of a Node.js HTTP get request with JavaScript?

To get data out of a Node.js HTTP get request with JavaScript, we can listen for the data event on the response.

For instance, we write

const callback = (response) => {
  let str = "";
  response.on("data", (chunk) => {
    str += chunk;
  });

  response.on("end", () => {
    console.log(str);
    // ...
  });
};

const req = http.request(options, callback).end();

to call http.request with the request options and a callback that calls response.one to listen for the 'data' event.

In the data event callback, we get the response chunk which has a chunk of the response body string.

We concatenate all the chunks into str so we get the full response body string.

When we get all the chunks, the end event is emitted and we log the value of str in the callback.

Conclusion

To get data out of a Node.js HTTP get request with JavaScript, we can listen for the data event on the response.

Categories
JavaScript Answers

How to subtract one month using moment.js and JavaScript?

Sometimes, we want to subtract one month using moment.js and JavaScript.

In this article, we’ll look at how to subtract one month using moment.js and JavaScript.

How to subtract one month using moment.js and JavaScript?

To subtract one month using moment.js and JavaScript, we can use the subtract method.

For instance, we write

const d = moment().subtract(1, "months").format("MMM YYYY");

to create a moment object with the current datetime with moment.

Then we call subtract with 1 and 'months' to subtract 1 month from the current date time in the moment object and return it.

And then we call format to return a formatted date string with the month and year.

Conclusion

To subtract one month using moment.js and JavaScript, we can use the subtract method.