Categories
JavaScript Answers

How to make HTTP requests with gzip/deflate compression with Node?

To make HTTP requests with gzip/deflate compression with Node, we set the gzip property.

For instance, we write

request(
  { method: "GET", uri: "http://www.example.com", gzip: true },
  (error, response, body) => {
    console.log(response.headers["content-encoding"]);
    console.log(body);
  }
);

to call request with an object with the gzip option set to true to make request with gzip compression.

Then we get the response headers from response.headers and the response body from body.

Categories
JavaScript Answers

How to fix Node and MySQL – ER_ACCESS_DENIED_ERROR Access denied for user ‘root’@’localhost’ error?

To fix Node and MySQL – ER_ACCESS_DENIED_ERROR Access denied for user ‘root’@’localhost’ error, we should make sure we connect with the right credentials.

For instance, we write

const mysql = require("mysql");
const connection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
});

to call createConnection with an object with the user userame and password password to the host host.

Categories
JavaScript Answers

How to load fonts with Webpack and font-face with JavaScript?

To load fonts with Webpack and font-face with JavaScript, we set the modules property to load the fonts.

For instance, we write

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpackConfig = require("./webpack.config");

module.exports = {
  //...
  module: {
    rules: [
      {
        test: /\.(sa|sc|c)ss$/,
        use: ["style-loader", "css-loader", "sass-loader"],
      },
      {
        test: /\.(png|woff|woff2|eot|ttf|svg)$/,
        use: ["url-loader?limit=100000"],
      },
    ],
  },
};

to set the module.rules property to load woff, woff2, eot, and ttf fonts.

And we set it to use url-loader to load the font files.

Categories
JavaScript Answers

How to fix webpack-dev-server Cannot find module ‘webpack’ error with JavaScript?

To fix webpack-dev-server Cannot find module ‘webpack’ error with JavaScript, we install some missing packages.

To install it, we run

npm install --save-dev webpack-dev-server 

to install webpack-dev-server to let us import the package.

Categories
JavaScript Answers

How to resolve Node Socket.io 404 (Not Found) error?

To resolve Node Socket.io 404 (Not Found) error, we connect to the URL and port of the Socket.io server.

For instance, we write

const app = express();
const server = app.listen(3000);
const io = require("socket.io").listen(server);

to call app.listen` to listen to port 3000.

Then we call listen with server to add socket.io to our app.

We can then connect to it at the URL with port 3000.