-1

I have a simple node server that had been running and handling requests smoothly until I came home for the holidays. Now, it appears to be loading, but does not respond to POST/GET requests. By this I mean that when I start my server (through calling node app.js) the console prints out that the server is listening on the specific port, and when I visit http://localhost:8000/, I see "hello" in response. However, when I try to make requests to my server via Postman or through an XMLHTTPRequest (things that worked smoothly before I moved locations), I get an error.

Is there something wrong with my code? Is it possible that my wifi and connection at home is different from my workspace?

See the important code in my server below:

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!');
});

thank you very much!

vchen04
  • 1
  • 1
  • 1
  • 4
    what error do you get? are you running Postman on the same host as the node server? – Joe Dec 30 '16 at 15:14
  • Yes, I am running Postman on the same host as the node server. However, I get a 0 when printing the status of my request in my onreadystatechange function. Another stackoverflow question (http://stackoverflow.com/questions/872206/http-status-code-0-what-does-this-mean-in-ms-xmlhttp) listed the possible reasons for this error as: Illegal cross origin request (see CORS) Firewall block or filtering The request itself was cancelled in code... Should I return to my workplace to continue working? – vchen04 Dec 30 '16 at 16:29
  • 1
    We need to see EXACTLY what error you get and what your client calling code is. Since you changed locations, this could be anything from a network access issue to a browser cross origin issue to a whole host of other things. You need to give us some clues and seeing the EXACT error you get would be a start. – jfriend00 Dec 30 '16 at 16:57
  • Did [my answer](https://stackoverflow.com/questions/41398428/node-server-not-responding-to-requests/41398503#41398503) below help you? If so then you can consider [accepting the answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). – rsp Jan 03 '17 at 18:02

1 Answers1

-1

I don't know what error do you get (this would be kind of important to know) but the code is fine. It works and it returns a correct response. See:

$ 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

The only thing I see is that it returns the content type of HTML instead of plain text so that may be a problem. You can change it to:

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

To have a content type for plain text.

rsp
  • 107,747
  • 29
  • 201
  • 177