亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這個Javascript“要求”是什么?

這個Javascript“要求”是什么?

慕娘9325324 2019-07-01 15:01:32
這個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()方法用于加載和緩存JavaScript模塊。因此,如果要將本地相對JavaScript模塊加載到Node.js應用程序中,只需使用require()方法。

例子:

var yourModule = require( "your_module_name" ); //.js file extension is optional


查看完整回答
反對 回復 2019-07-01
  • 3 回答
  • 0 關注
  • 361 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號