这个 JavaScript 的“要求"是什么? [英] What is this JavaScript "require"?
问题描述
我正在尝试让 JavaScript 读取/写入 PostgreSQL 数据库.我在 GitHub 上发现了这个 项目.我能够获得以下示例代码以在 Node 中运行.
I'm trying to get JavaScript to read/write to a PostgreSQL database. I found this project on GitHub. I was able to get the following sample code to run in 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 控制台,它只是说需要未定义".
Next I tried to make it run on a webpage, but nothing seemed to happen. I checked on the JavaScript console and it just says "require not defined".
那么这个要求"是什么?为什么它在 Node 中有效,而在网页中无效?
So what is this "require"? Why does it work in Node but not in a webpage?
此外,在我让它在 Node 中工作之前,我必须执行 npm install pg
.那是关于什么的?我查看了目录,没有找到文件 pg.它放在哪里,JavaScript 是如何找到它的?
Also, before I got it to work in Node, I had to do npm install pg
. What's that about? I looked in the directory and didn't find a file pg. Where did it put it, and how does JavaScript find it?
推荐答案
那么这个要求"是什么?
So what is this "require?"
require()
不是标准的一部分JavaScript API.但是在 Node.js 中,它是一个具有特殊用途的内置函数:加载模块.
模块是一种将应用程序拆分为单独文件的方法,而不是将所有应用程序放在一个文件中.这个概念也存在于其他在语法和行为上有细微差别的语言中,比如 C 的 include
、Python 的 import
等等.
Modules are a way to split an application into separate files instead of having all of your application in one file. This concept is also present in other languages with minor differences in syntax and behavior, like C's include
, Python's import
, and so on.
Node.js 模块和浏览器 JavaScript 之间的一大区别是如何从另一个脚本的代码访问一个脚本的代码.
One big difference between Node.js modules and browser JavaScript is how one script's code is accessed from another script's code.
在浏览器 JavaScript 中,脚本是通过
元素添加的.当它们执行时,它们都可以直接访问全局范围,即所有脚本之间的共享空间".任何脚本都可以在全局范围内自由定义/修改/删除/调用任何内容.
In browser JavaScript, scripts are added via the
<script>
element. When they execute, they all have direct access to the global scope, a "shared space" among all scripts. Any script can freely define/modify/remove/call anything on the global scope.
在 Node.js 中,每个模块都有自己的作用域.一个模块不能直接访问另一个模块中定义的东西,除非它选择公开它们.要从模块公开事物,必须将它们分配给 exports
或 module.exports
.一个模块要访问另一个模块的exports
或module.exports
,它必须使用require()
.p>
In Node.js, each module has its own scope. A module cannot directly access things defined in another module unless it chooses to expose them. To expose things from a module, they must be assigned to exports
or module.exports
. For a module to access another module's exports
or module.exports
, it must use require()
.
在您的代码中,var pg = require('pg');
加载 pg
模块,一个用于 Node.js 的 PostgreSQL 客户端.这允许您的代码通过 pg
变量访问 PostgreSQL 客户端 API 的功能.
In your code, var pg = require('pg');
loads the pg
module, a PostgreSQL client for Node.js. This allows your code to access functionality of the PostgreSQL client's APIs via the pg
variable.
为什么它在 node 中有效,而在网页中无效?
Why does it work in node but not in a webpage?
require()
、module.exports
和 exports
是 Node.js 特有的模块系统的 API.浏览器没有实现这个模块系统.
require()
, module.exports
and exports
are APIs of a module system that is specific to Node.js. Browsers do not implement this module system.
此外,在我让它在 node 中工作之前,我必须执行 npm install pg
.这是关于什么的?
Also, before I got it to work in node, I had to do
npm install pg
. What's that about?
NPM 是托管已发布 JavaScript 模块的包存储库服务.npm install
是一个命令,可让您从其存储库下载软件包.
NPM is a package repository service that hosts published JavaScript modules. npm install
is a command that lets you download packages from their repository.
它放在哪里,Javascript如何找到它?
Where did it put it, and how does Javascript find it?
npm cli 将所有下载的模块放在您运行 npm install
的 node_modules
目录中.Node.js 有关于模块如何查找其他模块的非常详细的文档,其中包括查找node_modules
目录.
The npm cli puts all the downloaded modules in a node_modules
directory where you ran npm install
. Node.js has very detailed documentation on how modules find other modules which includes finding a node_modules
directory.
这篇关于这个 JavaScript 的“要求"是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!