G.TongFei G.TongFei - 5 months ago 57
Node.js Question

Cannot GET /listUsers Nodejs Error

I'm using the tutorial found here: http://www.runoob.com/nodejs/nodejs-restful-api.html and added the following code.

var express=require('express');
var app=express();
var fs=require('fs');
app.get('listUsers',function(req,res){
fs.readFile(__dirname + "/" + "users.json",'utf-8',function(err,data){
console.log(data);
res.end(data);
});

});
var server=app.listen(8081,function(){
var host=server.address().address;
var port=server.address().port;
// console.log("应用实例,访问地址为 http://%s:%s",host,port);
})


After debug the server with IntelliJ IDEA test.js I'm getting an error stating Cannot GET / when I access localhost:8081/listUsers and I just wondered if anyone knew much about this error as Express and Node are new to me?

this is my json file:users.json

{
"user1" : {
"name" : "mahesh",
"password" : "password1",
"profession" : "teacher",
"id": 1
},
"user2" : {
"name" : "suresh",
"password" : "password2",
"profession" : "librarian",
"id": 2
},
"user3" : {
"name" : "ramesh",
"password" : "password3",
"profession" : "clerk",
"id": 3
}
}

Answer

you missed backward slash before listUsers route. this will certainly work

app.get('/listUsers',function(req,res){
   fs.readFile(__dirname + "/" + "users.json",'utf-8',function(err,data){
      console.log(data);
       res.end(data);
   });

});