linux 指令:
// 啟動 PostgreSQL
sudo service postgresql start
// 進入 PostgreSQL
sudo sudo -u postgres psql
// 更改密碼
postgres=# \password
// 新建新用戶與密碼
postgres=# create user loli superuser password 'a';
//-------------------------------------
var http = require('http');
var path = require('path');
var express = require('express');
var qs = require('querystring');
var pg = require('pg');
var app = express();
var server = http.createServer(app);
var conString = "postgres://postgres:a@localhost/my"; // "postgres://<使用者名稱>:<密碼>@<IP>/<資料庫名稱>";
app.use(express.static(path.resolve(__dirname, 'client')));
app.get('/',function(req, res){ // URL 的路徑 (根目錄)
pg.connect(conString, function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
// client.query("INSERT INTO users (name) values('AAA')");
client.query('SELECT * from users;', function(err, result) {
//call `done()` to release the client back to the pool
done();
if(err) {
client.end('error running query', err);
return;
}
var msg = '';
for(var i = 0; i < result.rows.length; i++){
msg += result.rows[i].name + '\n';
}
res.end(msg);
});
});
});
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
var addr = server.address();
console.log("伺服器已啟動 ", addr.address + ":" + addr.port);
});
留言列表