简体   繁体   English

Express NodeJS中的HTTP请求

[英]HTTP requests in Express NodeJS

I have the following code from https://github.com/chjj/tty.js/ : 我有来自https://github.com/chjj/tty.js/的以下代码:

  this.get("/hola", function(res) {
    iniciar();
  });

  function iniciar() {
    self.init();
  }
  iniciar();

going to localhost:8080/hola , it does not load. 转到localhost:8080/hola ,它不会加载。 localhost:8080 works perfectly. localhost:8080完美运行。 self.init() calls a function that, in turn, calls other functions. self.init()调用一个函数,该函数又调用其他函数。 The problem seems to be the following called function: 问题似乎是以下调用函数:

Server.prototype.initMiddleware = function() {
  var self = this
    , conf = this.conf;

  this.use(function(req, res, next) {
    var setHeader = res.setHeader;
    res.setHeader = function(name) {
      switch (name) {
        case 'Cache-Control':
        case 'Last-Modified':
        case 'ETag':
          return;
      }
      return setHeader.apply(res, arguments);
    };
    next();
  });

  this.use(function(req, res, next) {
    return self._auth(req, res, next);
  });

  this.use(term.middleware());
  this.use(this.app.router);
  this.use(express.static(__dirname + '/../static'));
};

According to express.js documentation: 根据express.js文档:

// a middleware sub-stack which prints request info for any type of HTTP request to /user/:id
app.use('/user/:id', function(req, res, next) {
  console.log('Request URL:', req.originalUrl);
  next();
}, function (req, res, next) {
  console.log('Request Type:', req.method);
  next();
});

So, it seems that there are "conflicts" between first app.get and the others app.use or this.use . 因此,似乎第一个app.get与其他app.usethis.use之间存在“冲突”。 How can I solve that? 我该如何解决?

it's because you are not returnig anything and then the browser is polling this until it return something. 这是因为您什么都不返回,然后浏览器会对此进行轮询,直到返回任何内容。

this.app.get("/hola", function(req, res) {
    res.writeHead(200, {'content-type':'text/html'});
    res.end('/*html code here*/');
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM