2013-03-26 97 views
0

我测试给了请求的URL,但是当我尝试:node.js的socket.io - 跟踪请求的URL

http://localhost:4000/foo 

的浏览器说:不能让/富

我的服务器代码:

var express = require('express'); 
var app = express(); 
var http = require('http'); 
var server = http .createServer(app); 
var io = require('socket.io').listen(server); 
var url = require('url'); 





server.listen(4000); 

app.get('/', function (request, response) { 
var pathname = url.parse(request.url).pathname; 
console.log("currentpathname: "+pathname); 
}); 

我想离开 “富” 为:

http://localhost:4000/foo 
+2

将app.get('/',。。。''改为'app.get('*',。。。 – generalhenry 2013-03-26 15:32:52

回答

0

快递只处理路径,你告诉它来处理,返回与消息404所有其他路径一样Cannot GET /foo

app.get('/'只处理'/'来处理,你需要使用app.get'*'

作为一个说明所有路径的规范

http.createServer(function (req, res) { 
    res.writeHead(200, {'Content-Type': 'text/plain'}); 
    res.end('Hello World\n'); 
}).listen(1337, '127.0.0.1'); 

相当于

express().all('*', function (req, res) { 
    res.writeHead(200, {'Content-Type': 'text/plain'}); 
    res.end('Hello World\n'); 
}).listen(1337, '127.0.0.1'); 

要得到怎样的感觉事情工作我推荐玩一下核心http模块。