Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js / Express - How to get variables defined in app.js in routes/index.js?

I'm new to Node.js and Express.

How can I access the variable created in "app.js" called "pg" in "routes/index.js"?

app.js

/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var pg = require('pg');
var conString = "postgres://someuser:somepass@localhost/postgres"

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

routes/index.js

/*
 * GET home page.
 */

exports.index = function(req, res){

    var client = new pg.Client(conString);
    client.connect(function(err) {
      if(err) {
        return console.error('could not connect to postgres', err);
      }
      client.query('SELECT NOW() AS "theTime"', function(err, result) {
        if(err) {
          return console.error('error running query', err);
        }
        console.log(result.rows[0].theTime);
        //output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
        client.end();
      });
    });

I got the error in the browser:

Express 500 ReferenceError: pg is not defined

Can you guys give me a clue?

Best Regards

like image 781
André Avatar asked Dec 30 '13 15:12

André


People also ask

How do you store local variables that can be access within the application in express JS?

5) What is the way to store local variables that can be accessed within the application? Answer: C is the correct option. We can store local variables that can be accessed within the application by using app. locals.

Which is the right way to define routes in node JS?

We define the routes by using the methods of this “app” object. This app object specifies a callback function, which is called when a request is received. We have different methods in app object for a different type of request.

How do I use Express router in node JS?

The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests. Multiple requests can be easily differentiated with the help of the Router() function in Express.


1 Answers

A simple way of passing anything to route handlers (whether they are declared in different files or not) in Express is to use app.locals:

// app.js
...
var app = express();
...
app.locals.someVar = someValue;
...

// somewhere else
module.exports.myhandler = function(req, res) {
  var someVar = req.app.locals.someVar;
  ...
};
like image 76
robertklep Avatar answered Nov 15 '22 07:11

robertklep