运行Nodejs应用程序而不监听端口

时间:2015-07-24 04:30:04

标签: node.js heroku express

我在Heroku上托管了一个Nodejs应用程序。代码中没有app.listen()。 Heroku如何运行服务器,而代码没有指定开始监听端口?

那么,如果没有app.listen,客户端和我们服务器之间的连接是否持续存在?或者对于来自客户端的每个请求,我们有一个临时连接在服务器发回结果后立即死亡?

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http = require('http');
var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//app.use('/', routes);
//app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


//var portNumber = process.env.PORT || 8080;

var portNumber = process.env.PORT || 9091;

var server = http.createServer(function(request, response){
  console.log('Connection');
  response.writeHead(200, {'Content-Type': 'text/html'});
  response.write('hellooooo');
  response.end();
});

server.listen(portNumber, function() {
  console.log('Server is listing on port : ' + portNumber);
});

module.exports = app;

0 个答案:

没有答案