Categories
JavaScript Answers

How to check if an environment variable is set in Node.js?

To check if an environment variable is set in Node.js, we use the in operator.

For instance, we write

if ("DEBUG" in process.env) {
  console.log(process.env.DEBUG);
} else {
  console.log("Env var IS NOT SET");
}

to check if the DEBUG property is in process.env with "DEBUG" in process.env to check if the DEBUG environment variable is set.

Categories
JavaScript Answers

How to use async await and .then together with JavaScript?

To use async await and .then together with JavaScript, we replace then with await.

For instance, we write

const f = async () => {
  try {
    const response = await fetch("http://no-such-url");
  } catch (err) {
    alert(err);
  }
};

to call fetch to return a promise with the response.

And we use await to get the response from the promise.

Categories
JavaScript Answers

How to return Mongoose results from the find method?

To return Mongoose results from the find method, we can promisfy Mongoose.

For instance, we write

const Promise = require("bluebird");
const mongoose = require("mongoose");

Promise.promisifyAll(mongoose);

const results = await Promise.props({
  users: users.find().execAsync(),
  articles: articles.find().execAsync(),
});
res.render("profile/profile", results);

to call promisifyAll to promisify mongoose.

Then we call Promise.props with an object with the promises as values.

We call find with execAsync to return a promise with the query resylts.

Then we get the results array with the returned results.

Categories
JavaScript Answers

How to add simple file upload to S3 using aws-sdk and Node/Express?

To add simple file upload to S3 using aws-sdk and Node/Express, we use the multer-s3 package.

For instance, we write

const express = require("express"),
  bodyParser = require("body-parser"),
  multer = require("multer"),
  s3 = require("multer-s3");

const app = express();

app.use(bodyParser.json());

const upload = multer({
  storage: s3({
    dirname: "/",
    bucket: "bucket-name",
    secretAccessKey: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    accessKeyId: "XXXXXXXXXXXXXXX",
    region: "us-east-1",
    filename: (req, file, cb) => {
      cb(null, file.originalname);
    },
  }),
});

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});

app.post("/upload", upload.array("upl"), (req, res, next) => {
  res.send("Uploaded!");
});

app.listen(3000, () => {
  console.log("Example app listening on port 3000!");
});

to call multer with an object that has storage set to the storage object created by s3.

We call s3 with an object with the AWS credentials, the region and the filename method to set the file name.

bucket has the bucket name. dirname is the upload file folder.

We call upload.array with the key name of the form data entry with the files to return the middleware to let us accept upload files.

Categories
JavaScript Answers

How to fix Please run npm cache clean error with Node?

To fix Please run npm cache clean error with Node, we run npm cache clean.

To do this, we run

npm cache clean --force

to clear the npm cache by force.