繁体   English   中英

节点服务器未响应请求

[英]node server not responding to requests

我有一个简单的节点服务器,一直运行并能顺利处理请求,直到我放假回家。 现在,它似乎正在加载,但是不响应POST / GET请求。 我的意思是,当我启动服务器(通过调用node app.js )时,控制台会打印出服务器正在侦听特定端口,并且当我访问http:// localhost:8000 /时 ,会看到“ hello”作为回应。 但是,当我尝试通过Postman或XMLHTTPRequest向我的服务器发出请求时(在移动位置之前,一切工作顺利),我收到了错误消息。

我的代码有问题吗? 我在家中的wifi和连接是否可能不同于我的工作空间?

请参阅下面服务器中的重要代码:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

app.get('/', function(req,res){
    res.send("hello");
})

app.listen(8000, function () {
   console.log('App listening on port 8000!');
});

非常感谢你!

我不知道您会遇到什么错误(知道这一点很重要),但是代码很好。 它有效,并且返回正确的响应。 看到:

$ curl -v http://localhost:8000/
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8000
> Accept: */*
> 
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: text/html; charset=utf-8
< Content-Length: 5
< ETag: W/"5-XUFAKrxLKna5cZ2REBfFkg"
< Date: Fri, 30 Dec 2016 15:18:36 GMT
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact
hello

我唯一看到的是它返回HTML的内容类型而不是纯文本,因此可能是一个问题。 您可以将其更改为:

app.get('/', function(req,res){
    res.set('Content-Type', 'text/plain');
    res.send("hello");
});

具有纯文本的内容类型。

暂无
暂无

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

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