为什么nodejs请求调用两次

我正在开发Node+ ansible模块,我需要通过ansible script来build立mysql docker镜像。 当我build立图像请求多次调用,如果请求get second time browserconnection was reset

注意:如果第二次请求完美地构build图像。 首先要求问题,同时浏览器去connection was reset.

我做了什么:

mysql.js:

  var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var bodyParser = require('body-parser'); var app = express(); var Ansible = require('node-ansible'); var yaml = require('node-yaml'); var timeout = require('infinite-timeout'); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: false})); app.use(express.static(path.join(__dirname, 'public'))); //app.use('/', index.jade); app.get('/', function (req, res) { tmp = ""; console.log(req.url); res.render('index', {title: ' Mysql Setup'}) }); var tmp = ""; var postcount; app.post('/setup', function (req, res) { postcount = Object.keys(req.body).length console.log(postcount); console.log(req.url); for (var key in req.body) { console.log(req.body[key]); if (req.body[key] == "mysql") { console.log("within mysql"); var playbook = new Ansible.Playbook().playbook("mysql"); playbook.on('stdout', function (data) { console.log(data.toString()); }); playbook.on('stderr', function (data) { console.log(data.toString()); }); var command = playbook.inventory('inventories/Mysql/hosts').verbose('v'); var promise = command.exec(); promise.then(function (successResult) { console.log("within mysql sucess"); tmp += "Build Mysql Image Process"; tmp += successResult.output; res.render('mysql_build_result', {data: tmp, title: 'Mysql Image Build Result'}); }, function (error) { tmp += error; console.log("within mysql error"); res.render('mysql_build_result', {data: tmp, title: 'Mysql Image Build Result Error'}); }); return; }else { console.log("No Request are matched"); } } }); var server = app.listen(3030, function () { var host = server.address().address var port = server.address().port console.log("Example app listening at http://%s:%s", host, port); }); 

Ansible脚本:

mysql.yml

  --- - name: Creates directory file: path=/opt/mysql state=directory owner=root group=root mode=0775 - name: Install Docker py command: pip install docker-py - name: Copy Docker Files to Remote machine synchronize: mode=push src=/var/www/html/nodeapp/dockerfiles dest=/opt/mysql/ - name: Build Mysql image and with buildargs docker_image: > path=/opt/mysql/dockerfiles/mysql name=mysql_node tag=mysql_node state=present 

Ansible日志:

  Example app listening at http://:::3030 / 1 /setup mysql within mysql Using /etc/ansible/ansible.cfg as config file PLAY [webservers] ************************************************************** TASK [Gathering Facts] ********************************************************* ok: [192.168.1.101] TASK [mysql : Creates directory] *********************************************** ok: [192.168.1.101] => {"changed": false, "gid": 0, "group": "root", "mode": "0775", "owner": "root", "path": "/opt/magento2", "size": 4096, "state": "directory", "uid": 0} TASK [mysql : Install Docker py] *********************************************** changed: [192.168.1.101] => {"changed": true, "cmd": ["pip", "install", "docker-py"], "delta": "0:00:00.283441", "end": "2017-08-02 15:34:55.003891", "rc": 0, "start": "2017-08-02 15:34:54.720450", "stderr": "You are using pip version 8.1.1, however version 9.0.1 is available.\nYou should consider upgrading via the 'pip install --upgrade pip' command.", "stderr_lines": ["You are using pip version 8.1.1, however version 9.0.1 is available.", "You should consider upgrading via the 'pip install --upgrade pip' command."], "stdout": "Requirement already satisfied (use --upgrade to upgrade): docker-py in /usr/lib/python2.7/site-packages\nRequirement already satisfied (use --upgrade to upgrade): requests>=2.5.2 in /usr/lib/python2.7/site-packages (from docker-py)\nRequirement already satisfied (use --upgrade to upgrade): six>=1.4.0 in /usr/lib/python2.7/site-packages (from docker-py)\nRequirement already satisfied (use --upgrade to upgrade): websocket-client>=0.32.0 in /usr/lib/python2.7/site-packages (from docker-py)\nRequirement already satisfied (use --upgrade to upgrade): urllib3==1.15.1 in /usr/lib/python2.7/site-packages (from requests>=2.5.2->docker-py)", "stdout_lines": ["Requirement already satisfied (use --upgrade to upgrade): docker-py in /usr/lib/python2.7/site-packages", "Requirement already satisfied (use --upgrade to upgrade): requests>=2.5.2 in /usr/lib/python2.7/site-packages (from docker-py)", "Requirement already satisfied (use --upgrade to upgrade): six>=1.4.0 in /usr/lib/python2.7/site-packages (from docker-py)", "Requirement already satisfied (use --upgrade to upgrade): websocket-client>=0.32.0 in /usr/lib/python2.7/site-packages (from docker-py)", "Requirement already satisfied (use --upgrade to upgrade): urllib3==1.15.1 in /usr/lib/python2.7/site-packages (from requests>=2.5.2->docker-py)"]} TASK [mysql : Copy Docker Files to Remote machine] **************************** root@192.168.1.101's password: ok: [192.168.1.101] => {"changed": false, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no --out-format=<<CHANGED>>%i %n%L /var/www/html/nodeapp_completed/dockerfiles root@192.168.1.101:/opt/magento2/", "msg": "", "rc": 0, "stdout_lines": []} TASK [mysql : Build Mysql image and with buildargs] **************************** 1 /setup mysql within mysql Using /etc/ansible/ansible.cfg as config file PLAY [webservers] ************************************************************** TASK [Gathering Facts] ********************************************************* ok: [192.168.1.101] TASK [mysql : Creates directory] *********************************************** ok: [192.168.1.101] => {"changed": false, "gid": 0, "group": "root", "mode": "0775", "owner": "root", "path": "/opt/magento2", "size": 4096, "state": "directory", "uid": 0} TASK [mysql : Install Docker py] *********************************************** changed: [192.168.1.101] => {"changed": true, "cmd": ["pip", "install", "docker-py"], "delta": "0:00:00.299737", "end": "2017-08-02 15:36:55.195024", "rc": 0, "start": "2017-08-02 15:36:54.895287", "stderr": "You are using pip version 8.1.1, however version 9.0.1 is available.\nYou should consider upgrading via the 'pip install --upgrade pip' command.", "stderr_lines": ["You are using pip version 8.1.1, however version 9.0.1 is available.", "You should consider upgrading via the 'pip install --upgrade pip' command."], "stdout": "Requirement already satisfied (use --upgrade to upgrade): docker-py in /usr/lib/python2.7/site-packages\nRequirement already satisfied (use --upgrade to upgrade): requests>=2.5.2 in /usr/lib/python2.7/site-packages (from docker-py)\nRequirement already satisfied (use --upgrade to upgrade): six>=1.4.0 in /usr/lib/python2.7/site-packages (from docker-py)\nRequirement already satisfied (use --upgrade to upgrade): websocket-client>=0.32.0 in /usr/lib/python2.7/site-packages (from docker-py)\nRequirement already satisfied (use --upgrade to upgrade): urllib3==1.15.1 in /usr/lib/python2.7/site-packages (from requests>=2.5.2->docker-py)", "stdout_lines": ["Requirement already satisfied (use --upgrade to upgrade): docker-py in /usr/lib/python2.7/site-packages", "Requirement already satisfied (use --upgrade to upgrade): requests>=2.5.2 in /usr/lib/python2.7/site-packages (from docker-py)", "Requirement already satisfied (use --upgrade to upgrade): six>=1.4.0 in /usr/lib/python2.7/site-packages (from docker-py)", "Requirement already satisfied (use --upgrade to upgrade): websocket-client>=0.32.0 in /usr/lib/python2.7/site-packages (from docker-py)", "Requirement already satisfied (use --upgrade to upgrade): urllib3==1.15.1 in /usr/lib/python2.7/site-packages (from requests>=2.5.2->docker-py)"]} TASK [mysql : Copy Docker Files to Remote machine] **************************** root@192.168.1.101's password: changed: [192.168.1.101] => {"actions": ["Built image m2_node_mysql_test2:m2_node_mysql_test from /opt/magento2/dockerfiles/mysql"], "changed": true, "image": {"Architecture": "amd64", "Author": "", "Comment": "", "Config": {"ArgsEscaped": true, "AttachStderr": false, "AttachStdin": false, "AttachStdout": false, "Cmd": ["mysqld"], "Domainname": "", "Entrypoint": ["docker-entrypoint.sh"], "Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.7", "MYSQL_MAJOR=5.6", "MYSQL_VERSION=5.6.37-1debian8"], "ExposedPorts": {"3306/tcp": {}}, "Hostname": "e24d0b5cccc1", "Image": "sha256:f64009628a0687e7e0c9de22b2c952bd5941cb401cf04375e06bb8af874c99ec", "Labels": {}, "OnBuild": [], "OpenStdin": false, "StdinOnce": false, "Tty": false, "User": "", "Volumes": {"/var/lib/mysql": {}}, "WorkingDir": ""}, "Container": "4be9441de29b690f8a7462c25c00290f89a2b01dfc110c09039140fae5f5e530", "ContainerConfig": {"ArgsEscaped": true, "AttachStderr": false, "AttachStdin": false, "AttachStdout": false, "Cmd": ["/bin/sh", "-c", "#(nop) CMD [\"mysqld\"]"], "Domainname": "", "Entrypoint": ["docker-entrypoint.sh"], "Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.7", "MYSQL_MAJOR=5.6", "MYSQL_VERSION=5.6.37-1debian8"], "ExposedPorts": {"3306/tcp": {}}, "Hostname": "e24d0b5cccc1", "Image": "sha256:f64009628a0687e7e0c9de22b2c952bd5941cb401cf04375e06bb8af874c99ec", "Labels": {}, "OnBuild": [], "OpenStdin": false, "StdinOnce": false, "Tty": false, "User": "", "Volumes": {"/var/lib/mysql": {}}, "WorkingDir": ""}, "Created": "2017-08-02T10:11:19.0629579Z", "DockerVersion": "1.10.3", "GraphDriver": {"Data": {"DeviceId": "3802", "DeviceName": "docker-253:3-11010052-8754256fd7cbda0fae8764a69bbeac1514463cfb85e7b7aff8ae2423195037b9", "DeviceSize": "10737418240"}, "Name": "devicemapper"}, "Id": "sha256:0d685cb2955e52f7fc67f1423d6e1f18b76036dfdbc3781edd9ca01fb227cd39", "Os": "linux", "Parent": "sha256:f64009628a0687e7e0c9de22b2c952bd5941cb401cf04375e06bb8af874c99ec", "RepoDigests": [], "RepoTags": ["m2_node_mysql_test2:m2_node_mysql_test"], "Size": 298292364, "VirtualSize": 298292364}} PLAY RECAP ********************************************************************* 192.168.1.101 : ok=5 changed=2 unreachable=0 failed=0 within mysql sucess 

build议我在我的代码中有什么错误。

我的问题:

  1. 如果我需要设置超时的手段,为什么第二次docker图像正确地build立。 为什么不是第一次。

  2. 浏览器networking选项卡不显示任何新的请求,但如何nodejs第二次调用,为什么第二个请求不捕获浏览器。

  3. 后端进程工作正常(但请求被调用两次),为什么浏览器去connection was reset