【发布时间】:2015-05-01 13:18:01
【问题描述】:
正在使用 Node.js 和 Express 创建一个 REST API。服务器从 localhost:xxxx 开始,但 localhost:xxxx/bits 返回 404 Not Found 错误。
var express = require('express');
var app = express();
app.get('/bits', function(req, res) {
console.log("Bits called");
});
app.get('/bits/:id', function(req, res) {
console.log("Bits ID called");
});
app.listen(xxxx);
console.log('Listening on port xxxx.');
帮助?
【问题讨论】:
-
由于路由或简单代码没有任何问题,您的服务器似乎很可能实际上并未在您访问的端口上运行或根本没有运行。您是否有机会尝试在您无权运行的低编号端口上运行?
-
服务器确实在运行。我试过这个:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(83, 'localhost'); console.log('Server running at http://127.0.0.1:83/');它打印了 Hello World。 -
我刚刚发现这个服务器是为
127.0.0.1:83运行的,但不是为localhost:83运行的。我的 system32 主机文件有127.0.0.1 localhost。可能是什么原因? -
在那台计算机上,在命令窗口中尝试
ping -4 localhost,看看它认为 localhost 是什么 IP 地址。 -
返回相同的,127.0.0.1。