Categories
JavaScript Answers

How to modify the Node.js request default timeout time?

To modify the Node.js request default timeout time, we set the timeout property.

For instance, we write

const http = require("http");
const server = http
  .createServer((req, res) => {
    setTimeout(() => {
      res.writeHead(200, { "Content-Type": "text/plain" });
      res.end("Hello World\n");
    }, 200);
  })
  .listen(1337, "127.0.0.1");

server.timeout = 20;
console.log("Server running at http://127.0.0.1:1337/");

to create a web server with createServer.

And we set the request timeout by setting the server.timeout property to a time milliseconds.

Categories
JavaScript Answers

How to store DB config in a Node.js and Express app?

To store DB config in a Node.js and Express app, we store it in an .env file.

For instance, we write

DATABASE_PASSWORD=pw
DATABASE_NAME=some_db

in our .env file.

Then we write

require("dotenv").config();
const password = process.env.DATABASE_PASSWORD;

to load the .env file with the dotenv package.

And then we get the DATABASE_PASSWORD environment variable value with process.env.DATABASE_PASSWORD.

Categories
JavaScript Answers

How to fix ‘gulp’ is not recognized as an internal or external command with JavaScript?

To fix ‘gulp’ is not recognized as an internal or external command with JavaScript, we install the gulp package.

To install it, we run

npm install -g gulp

to install gulp globally.

Categories
JavaScript Answers

How to get Mongoose connect error with Node?

To get Mongoose connect error with Node, we call connect with a callback as the 2nd argument.

For instance, we write

mongoose.connect("mongodb://localhost/dbname", (err) => {
  if (err) {
    throw err;
  }
});

to call connect with a callback as the 2nd argument.

We get connection errors from err if there’s any.

Categories
JavaScript Answers

How to fix libsass bindings not found when using node-sass in Node?

To fix libsass bindings not found when using node-sass in Node, we upgrade to gulp-sass 2.

To do this, we run

npm uninstall --save-dev gulp-sass
npm install --save-dev gulp-sass@2

to uninstall the original version of gulp-sass with npm uninstall.

Then we install gulp-sass 2 with npm install.