简体   繁体   English

Heroku Postgres在Node.js中连接

[英]Heroku Postgres Connecting in Node.js

I want to use a heroku database (already deployed in heroku) for my AngularJS application created using Yeoman. 我想为使用Yeoman创建的AngularJS应用程序使用一个heroku数据库(已经部署在heroku中)。 I want to persist my data that is currently an array of JSON objects that goes away when I refresh the page. 我要保留当前为JSON对象数组的数据,这些数据在刷新页面时会消失。 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. 我试图在此处遵循Heroku的Node.js指南( https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js ),但是它非常好用,没有示例,我相当服务器/数据库的新功能。 I have a 'web.js' file and the Procfile int my root directories for Node.js and heroku to read that file. 我有一个“ web.js”文件,Procfile int是我的根目录,Node.js和heroku可以读取该文件。 I have the "dependencies" already set but I am not sure what is happening in this code below that heroku provides 我已经设置了“依赖项”,但是我不确定heroku提供的以下代码中发生了什么

  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? 第三:如何使用它将当前为JSON对象数组的数据上载到heroku数据库中,而这些JSON对象已硬编码到代码中?


My web.js file 我的web.js文件

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. 该代码位于您的web.js文件中。

I'm pretty sure it's accessing your database and setting the results to the variable query so you can access the data. 我很确定它正在访问您的数据库并将结果设置为变量query以便您可以访问数据。

I think want you want for pushing the data is to look here , particularly at pg:push . 我认为想要推送数据的要看这里 ,尤其是pg:push

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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