Categories
JavaScript Answers

How to use a self signed certificate for a HTTPS Node.js server?

To use a self signed certificate for a HTTPS Node.js server, we set a few options.

For instance, we write

const options = {
  host: "localhost",
  port: 8000,
  path: "/api/v1/test",
  rejectUnauthorized: false,
  requestCert: true,
  agent: false,
};

https
  .createServer(options, (req, res) => {
    res.writeHead(200);
    res.end("OK\n");
  })
  .listen(8000);

to set rejectUnauthorized to false, requestCert to true, and agent to false to allow self signed certificates to be used on the https server.

Categories
JavaScript Answers

How to create line breaks in console.log() in Node.js?

To create line breaks in console.log() in Node.js, we log '\n'.

For instance, we write

console.log({ a: 1 }, "\n", { b: 3 }, "\n", { c: 3 });

to log line breaks with '\n'.

Categories
JavaScript Answers

How to write to S3 with Node AWS Lambda function?

To write to S3 with Node AWS Lambda function, we use the putObject method.

For instance, we write

const AWS = require("aws-sdk");

const putObjectToS3 = (bucket, key, data) => {
  const s3 = new AWS.S3();
  const params = {
    Bucket: bucket,
    Key: key,
    Body: data,
  };
  s3.putObject(params, (err, data) => {
    if (err) {
      console.log(err, err.stack);
    } else {
      console.log(data);
    }
  });
};

to call putObject to put the file at the Bucket with the Key.

Body has the file data.

And we get the file data from the data parameter in the callback.

Categories
JavaScript Answers

How to catch errors in JavaScript promises with a first level try-catch?

To catch errors in JavaScript promises with a first level try-catch, we use async and await.

For instance, we write

const func = async () => {
  try {
    const asyncResult = someAsyncAction();
    const someValue = await getSomeValue();
    doSomethingWith(someValue);
    await asyncResult;
  } catch (error) {
    console.error(error);
  }
};

to define the func function.

We use await to run the promises and wait for its result.

And we use a catch block to catch any promise rejections.

error would have the rejection reason.

Categories
JavaScript Answers

How to organize routes in Node.js?

To organize routes in Node.js, we move them to their own modules.

For instance, we write

const express = require("express");
const routes = require("./routes");
const user = require("./routes/user");
const http = require("http");
const path = require("path");

const app = express();

app.set("port", process.env.PORT || 3000);

app.get("/", routes.index);
app.get("/users/:id", user.getUser);

http.createServer(app).listen(app.get("port"), () => {
  console.log("Express server listening on port " + app.get("port"));
});

in app.js to add the routes from the other files into the app.

In routes/index.js, we write

exports.index = (req, res) => {
  res.render("index", { title: "Express" });
};

to export the index route.

And we add the getUser route into routes/user.js with

exports.getUser = (req, res) => {
  //...
};