我正在try 让JavaScript读取/写入PostgreSQL数据库.我在GitHub上找到了这个project.我能够在Node中运行以下示例代码.

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 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();
});

接下来,我试图让它在网页上运行,但似乎什么也没发生.我在JavaScript控制台上查看了一下,它只是说"require not defined".

那么这个"要求"是什么呢?为什么它可以在Node中工作,但不能在网页中工作?

而且,在我让它在Node中工作之前,我必须做npm install pg次.那是怎么回事?我在目录中查找,没有找到文件pg.它放在哪里,JavaScript是如何找到的?

推荐答案

那么这个"要求"是什么呢

require()不是标准JavaScript API的一部分.但在 node 中.js,这是一个内置函数,有一个特殊用途:to load modules.

模块是一种将应用程序拆分为单独文件的方法,而不是将所有应用程序都放在一个文件中.这个概念也出现在其他语言中,在语法和行为上有细微的差异,比如C的include、Python的import等等.

node 之间有一个很大的区别.js模块和浏览器JavaScript是如何从另一个脚本的代码访问一个脚本的代码.

  • 在浏览器JavaScript中,脚本是通过<script>元素添加的.当它们执行时,它们都可以直接访问全局作用域,即所有脚本之间的"共享空间".任何脚本都可以自由定义/修改/删除/调用全局范围内的任何内容.

  • 在Node.js中,每个模块都有自己的作用域.一个模块不能直接访问另一个模块中定义的内容,除非它 Select 公开它们.要公开模块中的内容,必须将它们分配给exportsmodule.exports.一个模块访问另一个模块的exportsmodule.exports,it must use 104.

在代码中,var pg = require('pg');加载pg模块,即 node 的PostgreSQL客户端.js.这允许您的代码通过pg变量访问PostgreSQL客户端API的功能.

为什么它在 node 中起作用,而在网页中不起作用?

require()module.exportsexports是特定于Node.js的模块系统的API.浏览器不实现此模块系统.

而且,在我让它在node中工作之前,我必须做npm install pg次.那是怎么回事?

NPM是一个软件包存储库服务,托管已发布的JavaScript模块.npm install是一个命令,允许您从其存储库中下载软件包.

它放在哪里,Javascript是如何找到它的?

npm cli将所有下载的模块放在您运行npm installnode_modules目录中.js有关于how modules find other modules的非常详细的文档,其中包括查找node_modules目录.

Javascript相关问答推荐

使用脚本标签时的JSDoc智能感知

如何将特定的字符串类型转换为TypScript中的字符串?

vue3类型脚本中可能未定义对象''

如何在非独立组件中使用独立组件?

Vue Quill css仅应用于我的第一个Quill Editor组件+如何自定义工具栏

我试图实现用户验证的reduxstore 和操作中出了什么问题?

Phaser 3 console. log()特定游戏角色的瓷砖属性

在服务器上放置了Create Reaction App Build之后的空白页面

如何控制Reaction路由加载器中的错误状态?

Eval vs函数()返回语义

更新动态数据中对象或数组中的所有值字符串

如何在Svelte中从一个codec函数中调用error()?

当我在Reaction中创建一个输入列表时,我的输入行为异常

OpenAI转录API错误请求

我想使用GAS和HTML将从Electron 表格中获得的信息插入到文本字段的初始值中

Cherrio JS返回父div的所有图像SRC

本地损坏的Java脚本

FindByIdAndUpdate在嵌套对象中创建_id

通过解构/功能组件接收props-prop验证中缺少错误"

未捕获的不变违规:即使在使用DndProvider之后也应使用拖放上下文