Categories
JavaScript Answers

How to read an Excel file using Node.js?

Sometimes, we want to read an Excel file using Node.js.

In this article, we’ll look at how to read an Excel file using Node.js.

How to read an Excel file using Node.js?

To read an Excel file using Node.js, we can use the node-xlsx module.

To install it, we run

npm i node-xlsx

Then we can use it by writing

const xlsx = require('node-xlsx');

const obj1 = xlsx.parse(__dirname + '/myFile.xlsx');
const obj2 = xlsx.parse(fs.readFileSync(__dirname + '/myFile.xlsx'));

to call xlsx.parse with the path of the Excel file and assign the returned object to obj1.

Then we call xlsx.parse with a buffer returned from reading the Excel file with readFileSync and assign that to obj2.

Conclusion

To read an Excel file using Node.js, we can use the node-xlsx module.

Categories
JavaScript Answers

How to set a base URL for Node.js Express app?

Sometimes, we want to set a base URL for Node.js Express app.

In this article, we’ll look at how to set a base URL for Node.js Express app.

How to set a base URL for Node.js Express app?

To set a base URL for Node.js app, we can call app.use with the base URL` and a route object.

For instance, we write

const express = require('express');
const app = express();
const router = express.Router();

router.use((req, res, next) => {
  console.log('%s %s %s', req.method, req.url, req.path);
  next();
});

router.use('/bar', (req, res, next) => {
  next();
});

router.use((req, res, next) => {
  res.send('Hello World');
});

app.use('/foo', router);

app.listen(3000);

to create a new router with express.Router.

Then we add some middlewares and routes with router.use.

And then we call app.use with '/foo' and router to nest router with /foo as the base URL for the routes.

Conclusion

To set a base URL for Node.js app, we can call app.use with the base URL` and a route object.

Categories
JavaScript Answers

How to access Express.js local variables in client side JavaScript?

Sometimes, we want to access Express.js local variables in client side JavaScript.

In this article, we’ll look at how to access Express.js local variables in client side JavaScript.

How to access Express.js local variables in client side JavaScript?

To access Express.js local variables in client side JavaScript, we can call res.render with an object with the variables.

Then we can reference them in the template.

For instance, we write

res.render('search-directory', {
  title: 'My Title',
  placeUrls: JSON.stringify(placeUrls),
});

to call res.render with an object with the local variables in the 2nd argument.

Then we write

search-directory.jade

script(type='text/javascript').
    const urls =!{placeUrls}

to add a script tag which assigns urls to the placeUrls JSON string.

The string will be interpolate as a JavaScript object.

Conclusion

To access Express.js local variables in client side JavaScript, we can call res.render with an object with the variables.

Categories
JavaScript Answers

How to add the sprintf equivalent for Node.js?

Sometimes, we want to add the sprintf equivalent for Node.js.

In this article, we’ll look at how to add the sprintf equivalent for Node.js.

How to add the sprintf equivalent for Node.js?

To add the sprintf equivalent for Node.js, we can use the util.format method.

For instance, we write

util.format('hello %s', 'world');

to call util.format with the format string and the values for the placeholders in the string.

Therefore, we see 'hello world' logged.

How to add the sprintf equivalent for Node.js?

To add the sprintf equivalent for Node.js, we can use the util.format method.

Categories
JavaScript Answers

How to download a file to disk from an AWS S3 bucket with Node.js?

Sometimes, we want to download a file to disk from an AWS S3 bucket with Node.js.

In this article, we’ll look at how to download a file to disk from an AWS S3 bucket with Node.js.

How to download a file to disk from an AWS S3 bucket with Node.js?

To download a file to disk from an AWS S3 bucket with Node.js, we can use the getObject method.

For instance, we write

const s3 = new AWS.S3();

const s3Params = {
  Bucket: 'your bucket',
  Key: 'path/to/the/file.ext'
};

s3.getObject(s3Params, (err, res) => {
  if (err === null) {
    res.attachment('file.ext');
    res.send(data.Body);
  } else {
    res.status(500).send(err);
  }
});

to call getObject with an object with the Bucket and Key to the file.

Then we get the data from res and call res.attachment to write the file data to the path.

And then we call res.send to put the file data into the file.

Conclusion

To download a file to disk from an AWS S3 bucket with Node.js, we can use the getObject method.