这个Javascript“要求”是什么?我试图让Javascript读取/写入PostgreSQL数据库。我发现了这个工程项目在GitHub上。我能够在节点中运行以下示例代码。var pg = require('pg'); //native libpq bindings = `var pg = require('pg').native`var conString = "tcp://postgres:1234@localhost/postgres";var client = new pg.Client(conString);client.connect();//queries are queued and executed one after another once the connection becomes availableclient.query("CREATE TEMP TABLE beatles(name varchar(10), height integer, birthday timestamptz)");client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['Ringo', 67, new Date(1945, 11, 2)]);client.query("INSERT INTO beatles(name, height, birthday) values($1, $2, $3)", ['John', 68, new Date(1944, 10, 13)]);//queries can be executed either via text/parameter values passed as individual arguments//or by passing an options object containing text, (optional) parameter values, and (optional) query nameclient.query({
name: 'insert beatle',
text: "INSERT INTO beatles(name, height, birthday) values($1, $2, $3)",
values: ['George', 70, new Date(1946, 02, 14)]});//subsequent queries with the same name will be executed without re-parsing the query plan by postgresclient.query({
name: 'insert beatle',
values: ['Paul', 63, new Date(1945, 04, 03)]});var query = client.query("SELECT * FROM beatles WHERE name = $1", ['John']);//can stream row results back 1 at a timequery.on('row', function(row) {
console.log(row);
console.log("Beatle name: %s", row.name); //Beatle name: John
console.log("Beatle birth year: %d", row.birthday.getYear()); //dates are returned as javascript dates
console.log("Beatle height: %d' %d\"", Math.floor(row.height/12), row.height%12); //integers are returned as javascript ints});//fired after last row is emittedquery.on('end', function() {
client.end();});接下来,我试图让它在网页上运行,但似乎什么也没有发生。我查看了Javascript控制台,它只说“RequireNotDefined”。那么这“要求”是什么呢?为什么它在节点中工作,而在网页中却不起作用?而且,在我让它在节点上工作之前,我必须做npm install pg..那是怎么回事?我查看了目录,没有找到PG文件。它把它放在哪里,Javascript是怎么找到它的?
3 回答
牛魔王的故事
TA贡献1830条经验 获得超3个赞
circle_object.js
:
var Circle = function (radius) { this.radius = radius}Circle.PI = 3.14Circle.prototype = { area: function () { return Circle.PI * this.radius * this.radius; }}
require
node> require('circle_object'){}node> Circle{ [Function] PI: 3.14 }node> var c = new Circle(3){ radius: 3 }node> c.area()
require()
require()
var yourModule = require( "your_module_name" ); //.js file extension is optional
添加回答
举报
0/150
提交
取消