Categories
Node.js Tips

Node.js Tips — Shell Commands, View Globals, Query with Sequelize

Spread the love

Like any kind of apps, there are difficult issues to solve when we write Node apps.

In this article, we’ll look at some solutions to common problems when writing Node apps.

Run an exe File in a Node.js App

We can run a .exe file in a Node app with the exec method.

For instance, we can write:

const exec = require('child\_process').execFile;

const run = () => {
  exec('program.exe', (err, data) => {
    console.log(err)
    console.log(data.toString());
  });
}

We created a run function to run program.exec with exec .

Once it’s run, the callback is called.

data has the result of running the program.

Escape a String for a Shell Command in Node

We can let the spawn function do the escaping for us.

We pass in the command as the first argument.

And the arguments for the command as the 2nd argument.

For instance, we can write:

const { spawn } = require('child\_process');

const ls = spawn('ls', \['-lh', '/tmp'\]);

ls.stdout.on('data', (data) => {
  console.log(data);
});

ls.stderr.on('data', (data) => {
  console.log(data);
});

ls.on('close', (code) => {
  console.log(code);
});

We get the spawn function from the child_process module.

Then we run the ls commands with some options.

The arguments are in the array.

Then we listen to the stdout and stderr for output.

stderr has the error output.

stdout has the standard output.

We listen to the close event to get the exit code of our command.

code has the exit code.

The arguments are escaped automatically.

We also shouldn’t let anyone pass in anything to those arguments even though they’re escaped.

Close Client Connection with socket.io

We can call socket.disconnect() on the client to disconnect from the server.

Or we can write:

socket.on('end', () => {
  socket.disconnect(0);
});

to listen to the end event emitted from the client on the server

And on the client, we write:

const io = io();
io.emit('end');

to emit the end event.

Express.js View Globals Variables

We can set our global variables for the views by putting them as properties of the app.locals property.

For instance, we can write:

app.locals.title = "app";
app.locals.version = 5;
app.locals.foo = () => {
  return "bar";
};

We add the title , version , and foo properties to app.locals .

They’re all merged together.

Then we can use them right in our Jade template by writing:

\=title
\=version
\=foo()

Then we get:

app
5
bar

displayed on the screen.

If we use connect-flash to provide messages to users, we can use app.set to set the message.

For instance, we can write:

app.use((req, res, next) => {
  app.set('message', req.flash('hello'));
  next();
});

Then we can access message by writing:

\=settings.message

in our template.

Wait Until Element is Displayed in the Node Version of Selenium WebDriver

With the Node version of Selenium WebDriver, we can call driver.wait to do the waiting.

For example, we can write:

driver.wait(() => {
  return driver.isElementPresent(webdriver.By.name("username"));
}, timeout);

We wait for an element with the name attribute username to be present until the timeout in milliseconds is reached.

Run Where Statement with Date with Sequelize

We can run a where statement with a Date object with Sequelize.

To do that, we just use the built-in operator constants as the key of where.

For instance, we can write:

const { Op } = require('sequelize');
const moment = require('moment');

model.findAll({
  where: {
    effective\_date: {
      \[Op.gte\]: moment().subtract(20, 'days').toDate()
    }
  }
})

We use the findAll method for the model class with an object that has the where property to run a where statement.

Then we can put the Op.gte key in the object with the column name as the key.

Op.gte means greater than or equal to.

We compute the date that we want to get the start range from with the moment’s subtract method and then toDate to convert it to the Date instance.

Then we look at records with effective_date 20 days before today or later.

There’s also Op.gt for greater than Op.lt for less than, Op.lte for less than or equal to and Op.ne for not equal.

Conclusion

We can run an .exe file with exec .

Sequelize lets us query with date ranges.

We can add global variables for templates with Express.

We can use wait to wait for something in Selenium.

Shell commands are escaped automatically with spawn .

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *