20 October 2019

NodeJS ExpressJS: Environment Variables [EX-18]

Environment Variables
Cách 1: sử dụng module npm i dotenv
/app.js
app.js
require('dotenv').config()
console.log(process.env.SESSION_SECRET);

const express = require('express')
const app = express()
const port = 3000
const cookieParser = require('cookie-parser');

const userRoute = require('./routes').user;
const authRoute = require('./routes').auth;
const middlware = require('./middlwares/auth.middlware');

app.set('view engine', 'pug')
app.set('views', './views');

// https://expressjs.com/en/4x/api.html#req.body
app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded

app.use(cookieParser(process.env.SESSION_SECRET));
app.use(express.static('public'));

// GET method route
app.get('/', middlware.authorize, function (req, res) {
    res.render('index', {
        title: 'Hey',
        message: "ExpressJS"
    })
})

/.env
.env
SESSION_SECRET=1235456

Cách 2: Sử dụng command line: 
SESSION_SECRET = 123456 npm start

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang