一起使用 Node-Static 和 Journey

发布于 2024-12-01 19:08:23 字数 1430 浏览 2 评论 0原文

我是第一次使用 Node.js。我使用 node-static 提供静态文件,并使用 旅程。不幸的是,两者似乎相互冲突,我不确定停止冲突的最佳方法。我的服务器如下所示:

var http = require('http');
var nodeStatic = require('node-static');
var journey = require('journey');

var fileServer = new nodeStatic.Server('./public');
var router = new journey.Router;


module.exports = http.createServer(function (req, res) {
  router.get('/api').bind(function (req, res) {
    res.send(200);
  });
var fileServer = new nodeStatic.Server('./public');
var router = new journey.Router;


module.exports = http.createServer(function (req, res) {
  router.get('/api').bind(function (req, res) {
    res.send(200);
  });

  req.addListener('end', function () {
    fileServer.serve(req, res);

    router.handle(req, '', function (result) {
      res.writeHead(result.status, result.headers);
      res.end(result.body);
    });
  });
});

问题在于 Journey 路由器发送 404 响应。我设法使其工作的唯一方法是在 end 侦听器中执行此操作:

  req.addListener('end', function () {
    router.handle(req, '', function (result) {
      if (result.status === 404) {
        fileServer.serve(req, res);
      }
      else {
        res.writeHead(result.status, result.headers);
        res.end(result.body);
      }
    });
  });

但这似乎不是我应该处理事情的方式。我做错了什么?

I'm using Node.js for the first time. I'm serving up static files with node-static and routing with journey. Unfortunately the two seem to conflict with each other, and I'm not sure of the best way to stop the conflict. My server looks like this:

var http = require('http');
var nodeStatic = require('node-static');
var journey = require('journey');

var fileServer = new nodeStatic.Server('./public');
var router = new journey.Router;


module.exports = http.createServer(function (req, res) {
  router.get('/api').bind(function (req, res) {
    res.send(200);
  });
var fileServer = new nodeStatic.Server('./public');
var router = new journey.Router;


module.exports = http.createServer(function (req, res) {
  router.get('/api').bind(function (req, res) {
    res.send(200);
  });

  req.addListener('end', function () {
    fileServer.serve(req, res);

    router.handle(req, '', function (result) {
      res.writeHead(result.status, result.headers);
      res.end(result.body);
    });
  });
});

The problem with this is that the Journey router sends a 404 response. The only way I've managed to make it work is by doing this in the end listener:

  req.addListener('end', function () {
    router.handle(req, '', function (result) {
      if (result.status === 404) {
        fileServer.serve(req, res);
      }
      else {
        res.writeHead(result.status, result.headers);
        res.end(result.body);
      }
    });
  });

but this doesn't seem like the way I should be handling things. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2024-12-08 19:08:23

我发现展示如何在旅程中使用节点静态的要点。我基本上是对的。不过,我很高兴听到任何替代解决方案!

I found a gist showing how to use node-static with journey. I basically had it right. I'd be happy to hear any alternative solutions though!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文