2

i am new at Javascript, and i still confuse about javascript and using json file. I working at one of my office project about sending a message, i have been complete all the code at back-end and front-end, but when the code was review with my project leader, he say to put all the credential to file json. To send a message / mail, i using nodemailer from node.js

Here is the credential i have to put in json file

const transporter = mail.createTransport({ //unresolved
  service: 'Gmail',
  host: 'smtp.gmail.com',
  auth: {
    user: 'no.replyxx@xx.org',
    pass: 'xxxxxx'
  }
});

I have put it into the json file, but when i call it at my javascript file, i don't know how to call it, i try to import a json file, but i just get bug for it can someone help me, precisely my question is how i write a credential to json file and how i use it at my javascript? Thanks

  • 1
    A common and secure way of dealing with this is to use a `.env` file and read into the node environment. A popular library for this is [dotenv](https://www.npmjs.com/package/dotenv) – dillon Aug 11 '20 at 03:09

4 Answers4

3

You should look into using dotenv

You can use it like so

// this will find the .env file at the root of your project
// and parse the entries into key/value pairs on the `process.env` object
require('dotenv').config()

const transporter = mail.createTransport({ //unresolved
  service: 'Gmail',
  host: 'smtp.gmail.com',
  auth: {
    user: process.env.MAIL_USER,
    pass: process.env.MAIL_PASS
  }
});

Create a .env file at the root of your project

// .env

MAIL_USER=no.replyxx@xx.org
MAIL_PASS=secret-password

Also, you should ensure that this does not get committed to your version control system.
If you're using git, create a .gitignore and make sure you have an entry for .env

// .gitignore

.env
dillon
  • 721
  • 6
  • 16
2

First thing, in JSON, there is no const. const comes from JS. If you want to save your credentials to a JSON file, it will contain something like this:

{
  "service": "Gmail",
  "host": "smtp.gmail.com",
  "auth": {
    "user": "no.replyxx@xx.org",
    "pass": "xxxxx"
  }
}

also, it needs double quotes and not single quotes.

After this all you need to do is require it

const credentials = require('./jsonCredentials.json');
illiteratewriter
  • 4,155
  • 1
  • 23
  • 44
1

First, you can import the content of a file using the fs library method called fs.readFileSync(filePath).

Then, you parse the content into a JSON object using the JSON.parse(content) method.

From there, you can have the JSON content as an object, and you can now use it in your createTransport method call.

Yee Hui Poh
  • 366
  • 2
  • 6
  • Can you recomend me any link as references? I don't know about fs library – Marvin Paulus Aug 11 '20 at 03:38
  • FS readFileSync: https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options JSON parse: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse – Yee Hui Poh Aug 11 '20 at 03:58
  • Also, as others have mentioned, using the env will be better. You can lookup on the dotenv library to inject the `.env` file into the `process.env` object. See the following for documentations https://www.npmjs.com/package/dotenv – Yee Hui Poh Aug 11 '20 at 04:00
  • Thankyou @Yee Hui Poh, my problem has been solved, with a little different ways, i will learn about fs library may it can usefull for my next project. thank you – Marvin Paulus Aug 11 '20 at 04:39
1

I just want to add that I recommend using .env files which are more secure.

But to answer your question, Since I'm assuming you're using ReactJS, you can do something like this:

config.js

export default const config = {
  service: 'Gmail',
  host: 'smtp.gmail.com',
  auth: {
    user: 'no.replyxx@xx.org',
    pass: 'xxxxxx'
  }
}

Then simply import to use it

import config from './config.js'

function myFunction()
{
  const transporter = mail.createTransport(config);
}
Jacob
  • 586
  • 6
  • 27
polcats
  • 144
  • 1
  • 6