मैं जावास्क्रिप्ट को PostgreSQL डेटाबेस को पढ़ने/लिखने के लिए प्राप्त करने का प्रयास कर रहा हूं। मुझे github पर यह project मिला। मैं नोड में चलाने के लिए निम्नलिखित नमूना कोड प्राप्त करने में सक्षम था।यह जावास्क्रिप्ट "आवश्यकता" क्या है?
var pg = require('pg'); //native libpq bindings = `var pg = require('pg').native`
var conString = "tcp://postgres:[email protected]/postgres";
var client = new pg.Client(conString);
client.connect();
//queries are queued and executed one after another once the connection becomes available
client.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 name
client.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 postgres
client.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 time
query.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 emitted
query.on('end', function() {
client.end();
});
अगला मैंने इसे वेबपृष्ठ पर चलाने की कोशिश की, लेकिन कुछ भी नहीं हुआ। मैंने जावास्क्रिप्ट कंसोल पर जांच की और यह सिर्फ "परिभाषित करने की आवश्यकता नहीं है" कहता है।
तो यह "आवश्यकता क्या है?" यह नोड में क्यों काम करता है लेकिन वेबपृष्ठ में नहीं?
इसके अलावा, मुझे इसे नोड में काम करने से पहले, मुझे npm install pg
करना पड़ा। यह किस बारे में है? मैंने निर्देशिका में देखा और फ़ाइल पीजी नहीं मिला। इसे कहां रखा, और जावास्क्रिप्ट इसे कैसे ढूंढता है?
आवश्यक जावास्क्रिप्ट का हिस्सा नहीं है, यह एक शब्द है जो नोडजेस में उपयोग किया जाता है। nodejs डोम नहीं है जिसे आप क्लाइंट साइड का उपयोग करते हैं। इसलिए एक स्क्रिप्ट जो नोडज के साथ काम कर सकती है वह ब्राउज़र में काम नहीं कर सकती है। क्या आप विंडो या दस्तावेज़ को नोडजेज़ में कॉल कर सकते हैं? नहीं, ब्राउज़र के साथ जरूरी है। – mpm
मैं उपरोक्त कोड कैसे बदलूं ताकि यह ब्राउज़र में काम करेगा? – neuromancer
आप सीधे वेब पेज से पीजी से बात नहीं कर सकते हैं; आपको एक सादा टीसीपी/आईपी सॉकेट खोलने में सक्षम होना चाहिए जिसे आप भेज सकते हैं और बाइनरी डेटा प्राप्त कर सकते हैं, और कोई भी वेब ब्राउज़र आपको ऐसा करने नहीं देगा। जिस पुस्तकालय का आप उल्लेख कर रहे हैं वह node.js का विस्तार है और क्लाइंट जावास्क्रिप्ट में काम नहीं करेगा। मैं दृढ़ता से अनुशंसा करता हूं कि आप क्लाइंट से अपने वेबसर्वर और JSON अनुरोध/उत्तरों के माध्यम से अपने PostgreSQL सर्वर से बात करें। –