一起使用 Node-Static 和 Journey
我是第一次使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现展示如何在旅程中使用节点静态的要点。我基本上是对的。不过,我很高兴听到任何替代解决方案!
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!