简体   繁体   中英

Heroku Postgres Connecting in Node.js

I want to use a heroku database (already deployed in heroku) for my AngularJS application created using Yeoman. I want to persist my data that is currently an array of JSON objects that goes away when I refresh the page. I am trying to follow Heroku's guide for Node.js here ( https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js ) but it is very shot, no examples, and I am fairly new to servers/databases. I have a 'web.js' file and the Procfile int my root directories for Node.js and heroku to read that file. I have the "dependencies" already set but I am not sure what is happening in this code below that heroku provides

  var pg = require('pg');
  pg.connect(process.env.DATABASE_URL, function(err, client) {
     var query = client.query('SELECT * FROM your_table');

     query.on('row', function(row) {
          console.log(JSON.stringify(row));
     });
   });

First: Where do I put this code?

Second: What is happening here?

and Third: How do I use it to upload my data that is currently an array of JSON objects that I hardcode into my code into the heroku database?


My web.js file

var gzippo = require('gzippo');
var express = require('express');
var app = express();

app.use(express.logger('dev'));
app.use(gzippo.staticGzip("" + MyApp + "/dist"));
app.listen(process.env.PORT || 9000);

My Procfile

web: node web.js

That code goes in your web.js file.

I'm pretty sure it's accessing your database and setting the results to the variable query so you can access the data.

I think want you want for pushing the data is to look here , particularly at pg:push .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM