Categories
JavaScript Answers

How to create an empty file in Node.js?

Sometimes, we want to create an empty file in Node.js.

In this article, we’ll look at how to create an empty file in Node.js.

How to create an empty file in Node.js?

To create an empty file in Node.js, we can call the fs.openSync method.

For instance, we write

fs.closeSync(fs.openSync(filePath, 'w'));

to call fs.openSync with the filePath of the file to write to and the 'w' (write) permission.

Then we close the returned file handle with fs.closeSync.

Conclusion

To create an empty file in Node.js, we can call the fs.openSync method.

Categories
JavaScript Answers

How to check if a variable exists with the Node.js Jade template engine?

Sometimes, we want to check if a variable exists with the Node.js Jade template engine.

In this article, we’ll look at how to check if a variable exists with the Node.js Jade template engine.

How to check if a variable exists with the Node.js Jade template engine?

To check if a variable exists with the Node.js Jade template engine, we can check if the property name with the variable name exists in the locals object.

For instance, we write

if locals.username
  p= username
else
  p No Username!

to check if the username template variable exists with if locals.username.

Conclusion

To check if a variable exists with the Node.js Jade template engine, we can check if the property name with the variable name exists in the locals object.

Categories
JavaScript Answers

How to upload a file using POST request in Node.js?

Sometimes, we want to upload a file using POST request in Node.js.

In this article, we’ll look at how to upload a file using POST request in Node.js.

How to upload a file using POST request in Node.js?

To upload a file using POST request in Node.js, we can submit our files in a form data object.

For instance, we write

const req = request.post(url, (err, resp, body) => {
  if (err) {
    console.log('Error!');
  } else {
    console.log('URL: ' + body);
  }
});

const form = req.form();
form.append('file', '<FILE_DATA>', {
  filename: 'myfile.txt',
  contentType: 'text/plain'
});

to call request.post with to make a POST request object req.

Then we call req.form to create a form form data object.

Then we call form.append with the name of the file input, the file data, and the file metadata respectively.

We get the response from body in the request.post callback.

Conclusion

To upload a file using POST request in Node.js, we can submit our files in a form data object.

Categories
JavaScript Answers

How to create global variables accessible in all views using Express and Node.js?

Sometimes, we want to create global variables accessible in all views using Express and Node.js.

In this article, we’ll look at how to create global variables accessible in all views using Express and Node.js.

How to create global variables accessible in all views using Express and Node.js?

To create global variables accessible in all views using Express and Node.js, we can set the app.locals property.

For instance, we write

app.locals.baseUrl = "http://www.example.com"

to set app.locals.baseUrl to a string.

Then we can access the baseUrl string with

req.app.locals.baseUrl

in our middleware functions.

Conclusion

To create global variables accessible in all views using Express and Node.js, we can set the app.locals property.

Categories
JavaScript Answers

How to convert CSV to JSON in Node.js?

Sometimes, we want to convert CSV to JSON in Node.js.

In this article, we’ll look at how to convert CSV to JSON in Node.js.

How to convert CSV to JSON in Node.js?

To convert CSV to JSON in Node.js, we can use the csvtojson module.

To install it, we run

npm i csvtohjson

Then we use it by writing

const csv = require("csvtojson");

csv()
  .fromStream(readableStream)
  .subscribe((jsonObj) => {
    console.log(jsonObj)
  })

const csvToJson = async () => {
  const jsonArray = await csv().fromFile(filePath);
}

to call fromStream with a readableStream to read the CSV data from the stream.

Then we call subscribe with a callback to get the JSON converted from the CSV with jsonObj.

Also, we can use csv().fromFile with a CSV filePath to convert the CSV file at filePath to a JSON object by returning a promise with the JSON object.

We then get the JSON result with await.

Conclusion

To convert CSV to JSON in Node.js, we can use the csvtojson module.