Categories
JavaScript Answers

How to use HTML as the view engine in Express?

Sometimes, we want to use HTML as the view engine in Express.

In this article, we’ll look at how to use HTML as the view engine in Express.

How to use HTML as the view engine in Express?

To use HTML as the view engine in Express, we can use the expres.static method.

For instance, we write

app.use(express.static(__dirname + '/public'));

to call express.static with the path to the HTML files we want to serve.

We don’t need a view engine to render the HTML.

Conclusion

To use HTML as the view engine in Express, we can use the expres.static method.

Categories
JavaScript Answers

How to POST data with request module on Node.js?

Sometimes, we want to POST data with request module on Node.js.

In this article, we’ll look at how to POST data with request module on Node.js.

How to POST data with request module on Node.js?

To POST data with request module on Node.js, we can use the request.post method.

For instance, we write

const request = require('request');
request.post({
  headers: {
    'content-type': 'application/x-www-form-urlencoded'
  },
  url: 'http://localhost/test2.php',
  body: "mes=heydude"
}, (error, response, body) => {
  console.log(body);
});

to call request.post with an object that has the request headers, url, and request body.

The callback in the 2nd argument runs when the request is finished.

We get the response body from body.

Conclusion

To POST data with request module on Node.js, we can use the request.post method.

Categories
JavaScript Answers

How to stop Mongoose from adding an s at the end of a collection name?

Sometimes, we want to stop Mongoose from adding an s at the end of a collection name.

In this article, we’ll look at how to stop Mongoose from adding an s at the end of a collection name.

How to stop Mongoose from adding an s at the end of a collection name?

To stop Mongoose from adding an s at the end of a collection name, we can set the collection property when we create the schema.

For instance, we write

const dataSchema = new Schema({
  //..
}, {
  collection: 'data'
})

to create a Schema instance with an object that sets the collection property.

The collection property is set to the name we want for the collection.

Conclusion

To stop Mongoose from adding an s at the end of a collection name, we can set the collection property when we create the schema.

Categories
JavaScript Answers

How to remove all files from directory without removing directory in Node.js?

Sometimes, we want to remove all files from directory without removing directory in Node.js.

In this article, we’ll look at how to remove all files from directory without removing directory in Node.js.

How to remove all files from directory without removing directory in Node.js?

To remove all files from directory without removing directory in Node.js, we can use the readdir method to get an array of file paths in the directory.

Then we can use unlink to remove all the files.

For instance, we write

const fs = require('fs');
const path = require('path');

const directory = 'test';

fs.readdir(directory, (err, files) => {
  if (err) {
    throw err;
  }

  for (const file of files) {
    fs.unlink(path.join(directory, file), err => {
      if (err) {
        throw err;
      }
    });
  }
});

to call readdir to get an array of files.

Then we loop through the files in the for-of loop.

In the loop body, we call fs.unlink with the path of each file returned by path.join to remove them.

Conclusion

To remove all files from directory without removing directory in Node.js, we can use the readdir method to get an array of file paths in the directory.

Then we can use unlink to remove all the files.

Categories
JavaScript Answers

How to set custom favicon in Express?

Sometimes, we want to set custom favicon in Express.

In this article, we’ll look at how to set custom favicon in Express.

How to set custom favicon in Express?

To set custom favicon in Express, we can use the server-favicon package.

To install it, we run

npm i server-favicon

Then we use it by writing

const favicon = require('serve-favicon');
const path = require('path');

//...

app.use(favicon(path.join(__dirname, 'public', 'images', 'favicon.ico')));

to call app.use with the middleware returned by favicon.

We call favicon with the path to the favicon.

Conclusion

To set custom favicon in Express, we can use the server-favicon package.