Categories
JavaScript Answers

How to get the width and height of an image in Node.js?

To get the width and height of an image in Node.js, we use the image-size package.

For instance, we write

const sizeOf = require("image-size");

sizeOf("images/funny-cats.png", (err, dimensions) => {
  console.log(dimensions.width, dimensions.height);
});

to call the sizeOf function to read the images/funny-cats.png image.

And we get the width and height from the dimensions parameter in the callback.

Categories
JavaScript Answers

How to fix Date.now().toISOString() throwing error “not a function” with JavaScript?

To fix Date.now().toISOString() throwing error "not a function" with JavaScript, we call toISOString on a date object.

For instance, we write

const now = new Date();
const isoString = now.toISOString();

to create a new Date object now.

Then we call now.toISOString to return the datetime string from the date.

Categories
JavaScript Answers

How to convert a directory structure in the filesystem to JSON with Node.js?

To convert a directory structure in the filesystem to JSON with Node.js, we use the directory-tree package.

To install it, we run

npm i directory-tree

Then we use it by writing

const directoryTree = require("directory-tree");

const tree = directoryTree("/some/path");
const filteredTree = directoryTree("/some/path", [".jpg", ".png"]);

to call directoryTree to return an object of the directory structure with the /some/path folder as root.

We can also call it with a 2nd argument to filter the items by extension.

Categories
JavaScript Answers

How to run a Node express server using webpack-dev-server?

To run a Node express server using webpack-dev-server, we run the webpack-dev-server command.

For instance, we write

const express = require("express");
const app = express();

require("./_routes")(app);
app.set("port", 8080);
app.listen(app.get("port"), () => {
  console.log("Node App Started");
});

to add an Express app in dev-server.js

Then in package.json, we write

{
  "scripts": {
    "start": "webpack --progress --colors",
    "dev-server": "node dev-server.js",
    "dev-client": "webpack-dev-server --port 3000"
  }
}

to add the dev-client script to run "webpack-dev-server in the dev-client script on port 3000.

Categories
JavaScript Answers

How to create Node.js server that accepts POST requests?

To create Node.js server that accepts POST requests, we use the http module.

For instance, we write

const http = require("http");
const server = http.createServer((request, response) => {
  response.writeHead(200, { "Content-Type": "textplain" });
  if (request.method === "GET") {
    response.end("received GET request.");
  } else if (request.method === "POST") {
    response.end("received POST request.");
  } else {
    response.end("Undefined request .");
  }
});

server.listen(8000);

to call createServer with a callback that checks the request method with the request.method property.

If it’s 'POST', then a post request is received.