Categories
JavaScript Answers

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

Sometimes, we want to store database config in Node.js and Express app.

In this article, we’ll look at how to store database config in Node.js and Express app.

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

To store database config in Node.js and Express app, we can use the dotenv module.

To install it, we run

npm i dotenv

Then we use it by writing

app.js

require('dotenv').config()

in our app’s entry point file.

And then in our .env file, we write

DATABASE_PASSWORD=mypw
DATABASE_NAME=some_db

Then we can use the environment variable values from process.env like

process.env.DATABASE_PASSWORD

Conclusion

To store database config in Node.js and Express app, we can use the dotenv module.

Categories
JavaScript Answers

How to get HTTP method in controller with Express.js?

Sometimes, we want to get HTTP method in controller with Express.js.

In this article, we’ll look at how to get HTTP method in controller with Express.js.

How to get HTTP method in controller with Express.js?

To get HTTP method in controller with Express.js, we can use the req.method property.

For instance, we write

const register = (req, res) => {
  if (req.method == "POST") {
    //...
  }
  res.render('user/registration.html', {
    form: form.toHTML()
  });
}

app.get('/register', register)
app.post('/register', register)

to define the register route handler function.

In it, we check the HTTP method used to make the request with req.method.

Then we can use the same route handler function for routes with different methods.

Conclusion

To get HTTP method in controller with Express.js, we can use the req.method property.

Categories
JavaScript Answers

How to add a where statement with date with Sequelize?

Sometimes, we want to add a where statement with date with Sequelize.

In this article, we’ll look at how to add a where statement with date with Sequelize.

How to add a where statement with date with Sequelize?

To add a where statement with date with Sequelize, we can use a JavaScript date object in where.

For instance, we write

const {
  Op
} = require('sequelize')

model.findAll({
  where: {
    start_datetime: {
      [Op.gte]: moment().subtract(7, 'days').toDate()
    }
  }
})

to query for items with the start_datetime value greater than or equal to 7 days or later than today with

[Op.gte]: moment().subtract(7, 'days').toDate()

We use the Op.gte operator to search for a column greater than or equal to a value.

Conclusion

To add a where statement with date with Sequelize, we can use a JavaScript date object in where.

Categories
JavaScript Answers

How to run app inside Docker as non-root user?

Sometimes, we want to run app inside Docker as non-root user.

In this article, we’ll look at how to run app inside Docker as non-root user.

How to run app inside Docker as non-root user?

To run app inside Docker as non-root user, we can use the adduser command to add a non-root user.

For instance, in our Dockerfile, we add

adduser --disabled-password --gecos '' r
adduser r sudo
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
su -m r -c /home/r/script.sh

to add the user r which is a non-root user.

We then run su with user r to run the /home/r/script.sh script.

Conclusion

To run app inside Docker as non-root user, we can use the adduser command to add a non-root user.

Categories
JavaScript Answers

How to add comments in Jade or Pug templates?

Sometimes, we want to add comments in Jade or Pug templates.

In this article, we’ll look at how to add comments in Jade or Pug templates.

How to add comments in Jade or Pug templates?

To add comments in Jade or Pug templates, we can add //- before our comment.

For instance, we write

doctype html

html(lang='en')
    body
        //- 
            This should be a comment

to add the This should be a comment comment in the body element.

Conclusion

To add comments in Jade or Pug templates, we can add //- before our comment.