我试图找出在我的express.js路由之间传递mysql连接(使用node-mysql)的最佳方法.我正在动态添加每个路由(在路由中使用for each文件循环),这意味着我不能只将连接传递给需要它的路由.我要么将它传递给每条路线,要么根本不传递.我不喜欢将它传递给那些不需要它的想法所以我创建了一个dbConnection.js,路由可以根据需要单独导入.问题是我不认为我正确地做到了.截至目前,我的dbConnection.js包含:
var mysql = require('mysql');
var db = null;
module.exports = function () {
if(!db) {
db = mysql.createConnection({
socketPath: '/tmp/mysql.sock',
user: '*********',
password: '*********',
database: '**********'
});
}
return db;
};
Run Code Online (Sandbox Code Playgroud)
我使用以下方法将其导入每条路线:
var db = require('../dbConnection.js');
var connection = new db();
Run Code Online (Sandbox Code Playgroud)
但我想这样做:
var connection = require('../dbConnection.js');
Run Code Online (Sandbox Code Playgroud)
然而,当我这样尝试时,我得到一个错误,当我尝试进行查询时,连接没有方法'查询'.
我已经建立了一个用Go编写的http服务器,每天有超过一千个访问者.我现在有一个累积的Goroutine问题.在一天的过程中,我似乎从http服务器上获得了一千多个新的Goroutines.
我不知道怎么会弄乱处理程序.
http.Handle("/", http.FileServer(http.Dir(config.htdocs_path)))
Run Code Online (Sandbox Code Playgroud)
下面是堆栈中的一个goroutines
goroutine 1582 [chan receive]:
net.(*pollServer).WaitRead(0xf84007f680, 0xf84066dea0, 0xf84007aa80, 0xb, 0x1, ...)
/home/ec2-user/go/src/pkg/net/fd.go:268 +0x73
net.(*netFD).Read(0xf84066dea0, 0xf840ec1000, 0x100000001000, 0x7f7effffffff, 0xf84007c0f0, ...)
/home/ec2-user/go/src/pkg/net/fd.go:428 +0x1ec
net.(*TCPConn).Read(0xf84068aff8, 0xf840ec1000, 0x100000001000, 0xf800000002, 0x0, ...)
/home/ec2-user/go/src/pkg/net/tcpsock_posix.go:87 +0xce
io.(*LimitedReader).Read(0xf840d1bc20, 0xf840ec1000, 0x100000001000, 0xdcb00000000, 0x0, ...)
/home/ec2-user/go/src/pkg/io/io.go:394 +0xc1
bufio.(*Reader).fill(0xf8405b0900, 0xdcb00000000)
/home/ec2-user/go/src/pkg/bufio/bufio.go:77 +0xf0
bufio.(*Reader).ReadSlice(0xf8405b0900, 0xf840d1bc0a, 0x0, 0x0, 0x0, ...)
/home/ec2-user/go/src/pkg/bufio/bufio.go:257 +0x1b6
bufio.(*Reader).ReadLine(0xf8405b0900, 0x0, 0x0, 0x0, 0x0, ...)
/home/ec2-user/go/src/pkg/bufio/bufio.go:283 +0x5b
net/textproto.(*Reader).readLineSlice(0xf840730660, 0xc0, 0x100000000, 0x7f7e00000001)
/home/ec2-user/go/src/pkg/net/textproto/reader.go:55 +0x4f
net/textproto.(*Reader).ReadLine(0xf840730660, 0xf84061f300, 0x0, 0x48411c)
/home/ec2-user/go/src/pkg/net/textproto/reader.go:36 +0x25
net/http.ReadRequest(0xf8405b0900, 0xf84061f300, 0x0, 0x0, 0x100000400ccf60, ...)
/home/ec2-user/go/src/pkg/net/http/request.go:457 …Run Code Online (Sandbox Code Playgroud) 我正在运行一个node.js服务器,它将在端口80和其他服务器上提供请求.显然,这需要以root身份运行应用程序(在Linux上).
以这篇文章(http://syskall.com/dont-run-node-dot-js-as-root)为例,很明显有一些简单的方法可以让节点作为非root用户运行,但我想知道是否有人对所建议的不同方法的优点/缺点有任何看法:
代码:在侦听端口80建立后,使用setuid()从root用户下载到非特权用户.
使用某种代理服务器将请求重定向到端口> 1024(因此不需要节点以root身份运行)
使用IP表转发到另一个端口(同上节点不会以root身份运行)
谢谢
在Go中,我有一些来自第三方API的JSON,然后我正在尝试解析它:
b := []byte(`{"age":21,"married":true}`)
var response_hash map[string]string
_ = json.Unmarshal(b, &response_hash)
Run Code Online (Sandbox Code Playgroud)
但它失败(response_hash变空)并且Unmarshal只能处理带引号的JSON:
b := []byte(`{"age":"21","married":"true"}`)
var response_hash map[string]string
_ = json.Unmarshal(b, &response_hash)
Run Code Online (Sandbox Code Playgroud)
有没有办法从第三方API读取第一个版本的JSON?
我有以下代码在Chrome(V8)下正常运行但在节点内部失败:
var id;
id = setTimeout("TimeoutHandler()", 10);
console.log ('SET');
function TimeoutHandler()
{
clearTimeout(id);
console.log ('CLEAR');
}
Run Code Online (Sandbox Code Playgroud)
Chrome输出:
SET
CLEAR
Run Code Online (Sandbox Code Playgroud)
Nodejs输出:
SET
timers.js:110
first._onTimeout();
^
TypeError: Property '_onTimeout' of object [object Object] is not a function
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?谢谢
不可否认,我是一个bash新手.我总是希望为我的shell脚本目的而使用Python.但是,我正试图让自己学习一些bash.我很好奇为什么以下代码不起作用.
sh -c "F=\"123\"; echo $F"
Run Code Online (Sandbox Code Playgroud) 在CloudFront灵活SSL后面设置时,无法通过https访问管理员.
管理员在通过http访问时工作正常,但是一旦我更改为安全https,它就会在重定向循环中结束.
我正在添加以下行wp-config.php以强制管理员中的SSL.
define('FORCE_SSL_ADMIN', true);
Run Code Online (Sandbox Code Playgroud) 我服务的背景图片相当大,我想让它永久缓存.我想我应该发送一个最大年龄为无穷大的缓存头.我怎样才能以干净和正确的方式做到这一点.我正在使用express(开箱即用)并且没有任何缓存模块.
我刚开始想出nodejs,我忘了把标志放在会话支持上
$ express -s somefolder
Run Code Online (Sandbox Code Playgroud)
我可以运行上面的命令而不会覆盖我已添加或更改的任何内容,或者我是否必须执行其他操作?
它不像向package.json添加新的依赖(stylus)并重新运行那样清晰 $ npm install
即时通讯使用Expressjs(NodeJS框架),我想将所有流量重定向到https,我不使用SSL证书,但Cloudflare灵活的ssl.
我正在使用这个中间件:
//FORCE SSL
app.use(function(req, res, next) {
if(!req.secure) {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
next();
});
Run Code Online (Sandbox Code Playgroud)
我以这种方式启动应用程序:
//Firing Up express
//====================================================================
app.set('port', process.env.PORT || 80);
var server = app.listen(app.get('port'), function() {
console.log('[-]');
console.log(('[+] Express server listening on port '+ server.address().port).green);
console.log('[-]');
});
Run Code Online (Sandbox Code Playgroud)
应用程序重定向到https://但无法加载
谷歌Chrome控制台说:
Failed to load resource: net::ERR_CONNECTION_REFUSED https://beta.domain.io/
Failed to load resource: net::ERR_CONNECTION_CLOSED https://beta.domain.io/
Failed to load resource: net::ERR_CACHE_MISS
Run Code Online (Sandbox Code Playgroud)
有人能指出我正确的方向吗?
提前致谢.
我正在构建一个Ansible playbook,在将playbook应用到远程服务器之前,我正在使用Vagrant作为测试平台.
我在让Synchronize工作时遇到问题.作为部署的一部分,我需要将一些文件移动到服务器.
这是我的剧本.我把它shell: whoami放在那里以确保命令以root身份运行.
---
- hosts: all
sudo: yes
tasks:
- name: who am I
shell: whoami
- name: Sync up www folder
synchronize: src=www dest=/var
Run Code Online (Sandbox Code Playgroud)
当我运行这个我得到这个:
failed: [default] => {"cmd": "rsync --delay-updates -FF --compress --archive --rsh 'ssh -i /Users/dan/.vagrant.d/insecure_private_key -o StrictHostKeyChecking=no -o Port=2222' --out-format='<<CHANGED>>%i %n%L' www vagrant@127.0.0.1:/var", "failed": true, "rc": 23}
msg: rsync: recv_generator: mkdir "/var/www" failed: Permission denied (13)
*** Skipping any contents from this failed directory ***
rsync error: some files/attrs were not …Run Code Online (Sandbox Code Playgroud) 我正在使用运行ubuntu和nodejs的ec2服务器.我认为这会创建一个有效的服务器,如果我转到我的ec2网址,它会响应.
var http = require("http");
var port = 80;
var serverUrl = "0.0.0.0";
console.log("Starting web server at " + serverUrl + ":" + port); http.createServer(
function(req, res) {
timestamp = new Date();
console.log("Request for URL " + req.url + " received at " + timestamp);
if (req.url === '/error') {
console.log('Throwing an error');
throw "Oops";
}
res.end("Hello World " + timestamp);
}).listen(port, serverUrl);
Run Code Online (Sandbox Code Playgroud)
我已经使用了nodejs并且特意表达了一段时间,但从未试图在vps上自己部署它,任何建议都表示赞赏.
node.js ×8
express ×4
go ×2
amazon-ec2 ×1
ansible ×1
bash ×1
caching ×1
cloudflare ×1
http ×1
http-headers ×1
https ×1
iptables ×1
javascript ×1
json ×1
knex.js ×1
node-mysql ×1
root ×1
session ×1
ssl ×1
v8 ×1
vps ×1
wordpress ×1