我正在使用此处的教程:http://www.runoob.com/nodejs/nodejs-restful-api.html并添加了以下代码。
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);
})
使用IntelliJ IDEA test.js调试服务器后我收到一条错误,说明无法获取/当我访问localhost:8081 / listUsers时我只是想知道是否有人对这个错误知之甚少,因为Express和Node对我来说是新手?
这是我的json文件: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
}
}
答案 0 :(得分:1)
你在listUsers路由之前错过了反斜杠。这肯定会起作用
@Override
public Observable<List<MessageEntity>> messages(String accountId, int messageType) {
return mMessageService.getLikeMessages(messageType)
.onErrorResumeNext(mTokenTrick.
refreshTokenAndRetry(mMessageService.getLikeMessages(messageType)));
}