title: Node.js connect to tcp socket after rendering express
body: <p>I make node.js web server and tcp client and java tcp server. (just for study socket and web programming) I want to make program that user connects to webpage and input some information, then node sends data to java tcp server and gets echoed data and finally renders data to webpage.</p>
  
  <p>My problem is that node renders webpage first and sends data to java tcp server. I want to make it sequentially. How can I solve it?</p>
  
  <p>Here's my code</p>
  
  <pre><code>var net = require('net');
  var rcvData;
  var socket;
  
  function getConnection(connName) {
      var client = net.connect({ port: 5000, host: '127.0.0.1' }, function () {
          console.log(connName + ' Connected: ');
          console.log('   local = %s:%s', this.localAddress, this.localPort);
          console.log('   remote = %s:%s', this.remoteAddress, this.remotePort);
          this.setEncoding('utf8');
  
          this.on('data', function (data) {
              rcvData = JSON.parse(data);
              console.log("From Server: " + rcvData.year);
              this.end();
          });
  
          this.on('end', function () {
              console.log(connName + ' Client disconnected');
          });
          this.on('error', function (err) {
              console.log('Socket Error: ', JSON.stringify(err));
          });
          this.on('timeout', function () {
              console.log('Socket Timed Out');
          });
          this.on('close', function () {
              console.log('Socket Closed');
          });
      });
      return client;
  }
  
  function writeData(socket, data) {
      var success = !socket.write(data);
      if (!success) {
          (function (socket, data) {
              socket.once('drain', function () {
                  writeData(socket, data);
              });
          })(socket, data);
      }
  }
  
  // http server
  var promise = require('promise');
  var express = require('express');
  var app = express();
  var path = require('path');
  var bodyParser = require('body-parser');
  var futures = require('futures');
  var sequence = futures.sequence();
  
  app.locals.pretty = true;
  
  app.set('view engine', 'pug');
  app.set('views', path.join(__dirname, 'views'));
  
  app.use(express.static('public'));
  app.use(bodyParser.urlencoded({ extended: false }));
  
  app.get('/', function (req, res) {
      res.render('index');
  });
  
  app.listen(8080, function () {
      console.log('http server&gt; listening on port 8080...');
  
  });
  
  app.post('/result', function (req, res) {
      sequence
          .then(function(next) {
              socket = getConnection("Java TCP Server");
              next(null, 1);
          })
          .then(function (next) {
              var jsonData = {
                  name: req.body.name,
                  year: req.body.year
              };
              writeData(socket, JSON.stringify(jsonData));
              next(null, 2);
          })
          .then(function (next) {
              console.log('render function enter');
              console.log('rcvData : ' + rcvData);
              res.render('result', {
                  name: rcvData.name,
                  year: rcvData.year
              });
              next(null, 4);
          });
  });
  </code></pre>