Categories
JavaScript Answers

How to add “All Rights Reserved” license in package.json?

Sometimes, we want to add "All Rights Reserved" license in package.json.

In this article, we’ll look at how to add "All Rights Reserved" license in package.json.

How to add "All Rights Reserved" license in package.json?

To add "All Rights Reserved" license in package.json, we add

{
  "license": "UNLICENSED"
}

into package.json

This will let us denying grant rights to use a private or unpublished package under any terms.

Conclusion

To add "All Rights Reserved" license in package.json, we add

{
  "license": "UNLICENSED"
}
Categories
JavaScript Answers

How to select a specific field with Mongoose find?

Sometimes, we want to select a specific field with Mongoose find.

In this article, we’ll look at how to select a specific field with Mongoose find.

How to select a specific field with Mongoose find?

To select a specific field with Mongoose find, we can use the schema find method with the field we want to select.

For instance, we write

dbSchemas.SomeValue.find({}, 'name', (err, someValue) => {
  if (err) {
    return
  }
  console.log(someValue);
});

to call dbSchemas.SomeValue.find to select the 'name' property.

Then someValue would be the value of the name property.

Conclusion

To select a specific field with Mongoose find, we can use the schema find method with the field we want to select.

Categories
JavaScript Answers

How to get rid of X-Powered-By response header in Node.js Express?

Sometimes, we want to get rid of X-Powered-By response header in Node.js Express.

In this article, we’ll look at how to get rid of X-Powered-By response header in Node.js Express.

How to get rid of X-Powered-By response header in Node.js Express?

To get rid of X-Powered-By response header in Node.js Express, we can use the app.disable method.

For instance, we write

app.disable('x-powered-by');

to disable the 'x-powered-by' option which removes the X-Powered-By response header in our Express app.

Conclusion

To get rid of X-Powered-By response header in Node.js Express, we can use the app.disable method.

Categories
JavaScript Answers

How to serve an image using Node.js?

Sometimes, we want to serve an image using Node.js.

In this article, we’ll look at how to serve an image using Node.js.

How to serve an image using Node.js?

To serve an image using Node.js, we can use the middleware returned by express.static.

For instance, we write

const path = require('path');
const express = require('express');
const app = express();

const dir = path.join(__dirname, 'public');

app.use(express.static(dir));

app.listen(3000, () => {
  console.log('Listening on http://localhost:3000/');
});

to call app.use with the middleware returned by express.static called with the dir of the image files.

This will expose the dir folder to the public.

Conclusion

To serve an image using Node.js, we can use the middleware returned by express.static.

Categories
JavaScript Answers

How to read the contents of a Node.js stream into a string variable?

Sometimes, we want to read the contents of a Node.js stream into a string variable.

In this article, we’ll look at how to read the contents of a Node.js stream into a string variable.

How to read the contents of a Node.js stream into a string variable?

To read the contents of a Node.js stream into a string variable, we can use the Buffer.concat method.

For instance, we write

const chunks = [];

readStream.on("data", (chunk) => {
  chunks.push(chunk);
});

readStream.on("end", () => {
  res.send(Buffer.concat(chunks));
});

to push the chunk into chunks when data received in the data event callback.

Then we call Buffer.concat with chunks to combine the chunks in the end event callback when we’re done reading the stream to return a file.

Conclusion

To read the contents of a Node.js stream into a string variable, we can use the Buffer.concat method.