Categories
JavaScript Answers

How to ping from a Node.js app?

To ping from a Node.js app, we call exec.

For instance, we write

const sys = require("sys");
const exec = require("child_process").exec;

exec("ping -c 3 localhost", (error, stdout, stderr) => {
  sys.puts(stdout);
});

to call exec to run the ping command.

And then we get the output from stdout in the callback.

Categories
JavaScript Answers

How to make synchronous database queries with Node.js?

To make synchronous database queries with Node.js, we use the synchronize module.

For instance, we write

const express = require("express");
const bodyParser = require("body-parser");
const mysql = require("mysql");
const sync = require("synchronize");

const db = mysql.createConnection({
  host: "localhost",
  user: "user",
  password: "password",
  database: "database",
});

db.connect((err) => {
  if (err) {
    console.error("error connecting: " + err.stack);
    return;
  }

  const save = async () => {
    const post = { id: newId };
    const query = sync.await(
      db.query("INSERT INTO mainTable SET ?", post, sync.defer())
    );
    const newId = query.insertId;
    const post = { foreignKey: newId };
    db.query("INSERT INTO subTable SET ?", post, (err, result) => {
      if (err) {
        throw err;
      }
    });
  };
});

to define the save function.

In it, we call sync.await to wait for the query to finish before we move to the next line of code.

We have the save function in the connect callback so we only make queries when the connection is established.

Categories
JavaScript Answers

How to add basic Ajax send/receive with Node.js?

To add basic Ajax send/receive with Node.js, we can use Express and fetch.

For instance, we write

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

app.get("/string", (req, res) => {
  const strings = ["foo", "bar", "baz"];
  const n = Math.floor(Math.random() * strings.length);
  res.send(strings[n]);
});
app.listen(8001);

to add the get /string route which returns a random string from the strings array as the response body with res.send.

Then we write

const res = await fetch("/string");
const string = await res.text();

to make a get request to /string with fetch.

And get the response body with res.text.

Categories
JavaScript Answers

How to fix Cordova Unable to load platformapi with JavaScript?

To fix Cordova Unable to load platformapi with JavaScript, we remove the platforms.

To do this, we run

ionic cordova platform rm browser/android/ios

to remove the platforms with platform rm.

Then we run

ionic cordova run --emulator

to run the emulator.

Categories
JavaScript Answers

How to fix TypeError: Request path contains unescaped characters error with Node?

To fix TypeError: Request path contains unescaped characters error with Node, we call the encodeURI function.

For instance, we write

const uri = "my test.asp?name=ståle&car=saab";
const res = encodeURI(uri);

to call encodeURI to return an escaped version of the uri string.