I can not access process.env nodejs

0

I am starting to program in node, and as part of my tasks I have to program an app with token authentication.

But I am stuck in the part where it generates it, because if I pass the direct text in the "key" field of the jwt-simple if it generates the token without problems ... but if I try to pass the process content to it .env tells me that it requires the key.

app.js

var express = require('express');  
var bodyparser = require('body-parser');  
var app = express();  
app.use(bodyparser.urlencoded({extended: true}));  
app.use(bodyparser.json());  
var connection = require('./connection');  
var routes = require('./routes');  
var server = app.listen(8000, function() {  
  console.log('Server listening on port ' + server.address().port);  
});
connection.init();  
routes.configure(app);

config.js

module.exports={  
    TOKEN_SECRET:process.env.TOKEN_SECRET || "tokenultrasecreto"  
};  

post.js

app.post('/todo/search/', function(req, res){       
var enviado=req.body.name;          
        todo.busca(req, res);  
    });

todo.js

this.busca = function(req,res) {  
    var id=req.body.name;  
    var idmy;  
    var name;  
    var pass=req.body.pass;  
    var list = [];  
    connection.acquire(function(err, con)/*1*/ {  
var cadena='select * from todo_list where name = \''+[id]+'\' and password=\''+[pass]+'\';';  
          console.log(cadena);  
          con.query(cadena, function(err, result) {  
        con.release();      
        console.log(id);        
        if (err) {  
            throw err;  
        } else {      
            idmy=result;  
            if(idmy.length > 0){  
                name=idmy[0].name;  
                pass=idmy[0].pass;  
                res.status(400).send({message:'Eureka!', token: service.createToken(id)});  
            }else{  
                console.log('Registro no encontrado');  
                res.status(400).send({message:'Registro no encontrado'});               
            }  
        }  
      });  
    });  

  };

var jwt=require('jwt-simple');  
var moment=require('moment');  
var config=('./config');  
var algorithm = 'HS256';  
exports.createToken=function(user){  
    var payload={  
        sub:user.id,  
        iat:moment().unix(),  
        exp:moment().add(1, "days").unix(),  
    };  
    console.log('Llega a services');  
    console.log(config.TOKEN_SECRET);  
    return jwt.encode(payload, config.TOKEN_SECRET);  
    //return jwt.encode(payload, 'Hola');  
};  

I hope you do not have too many beginner mistakes hahaha Greetings !!

    
asked by Hamoru-San Bgp 28.01.2017 в 01:02
source

1 answer

0

Yes you can access the environment variables. Your problem is that you are not really importing%% of%:

var config=('./config');

To import a module using CommonJS the function config.js is used:

const config = require('./config'); 

If you are using native modules of JS (ES6) the words require and% are used % co:

import config from './config';
    
answered by 28.01.2017 / 10:53
source