在 OSX Lion 上全新的 Silkjs 网络安装:
mschwartz@dionysus:~/src$ curl http://silkjs.org/install-osx.sh | sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1666 100 1666 0 0 3553 0 --:--:-- --:--:-- --:--:-- 79333
Installing SilkJS in /usr/local/silkjs
You may be asked to enter your password.
This is so the /usr/local directory can be made writable for the install.
Password:
Downloading SilkJS
######################################################################## 100.0%
Installation complete.
You need to add /usr/local/bin to your PATH environment variable.
This can be done by adding this line to your .bashrc file:
export PATH=/usr/local/bin:$PATH
You can run SilkJS with the following command:
$ silkjs
You can run the SilkJS HTTP Server with the following command:
$ httpd-silk.js yourapp.js
For instructions on setting up a WWW site for httpd-silk.js, see
http://silkjs.org/http-server/
现在看看它的工作原理:
mschwartz@dionysus:~/src$ silkjs
SilkJS> console.dir(require('MySQL'));
undefined line 1 (eval):
function () {
this.queryCount = 0;
this.handle = null;
}
undefined
SilkJS>
请注意,console.dir() 显示了一个函数。其余方法定义为函数(构造函数)的属性。所以你必须构造一个 MySQL() 对象:
SilkJS> var mysql = require('MySQL');
SilkJS> m = new mysql();
SilkJS> console.log(m.connect)
function (host, user, passwd, db) {
if (!this.handle) {
host = host || Config.mysql.host;
user = user || Config.mysql.user;
passwd = passwd !== undefined ? passwd : Config.mysql.passwd;
db = db || Config.mysql.db;
this.handle = mysql.connect(host, user, passwd, db);
}
}
undefined
SilkJS>
现在,为了连接,您必须传入主机、用户、密码和数据库。或者,如果您在 httpd 环境中运行,则设置 Config.mysql,如下所示: Config.mysql = { host: 'localhost', // 或其他运行 MySQL 服务器的主机 user: 'someuser', // 用户名在 MySQL 服务器中进行身份验证 passwd: 'whatever', // MySQL 服务器中用户名的密码 db: 'database_name" // MySQL 服务器中要连接的数据库名称 };
然后 HTTP 服务器将创建一个可从请求到请求的全局 SQL 对象。这是自动的,您可以只使用 SQL.getDataRows()、SQL.getScalar() 等。