TutorialsPoint - RESTful API example not working - Node.js

I am trying to make this RESTful example in my browser, but it continues to appear as inaccessible.

The .js server uses a call to store a host variable that registers as ::

 var host = server.address().address

Server.js

var express = require('express');
var app = express();
var fs = require("fs");

app.get('/listUsers', function (req, res) {
   fs.readFile( __dirname + "/" + "users.json", 'utf8', 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(host);

  console.log("Example app listening at http://%s:%s", host, port)

})

user.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
   }
}

Please help me fix this. Server address is displayed as:

Thanks Sam

0
source share
1 answer

The application express()uses a module httpfrom Nodejs, here it inherits the functionality of the function app.listen().

Here is an excerpt from server.address()and server.listen():

server.listen (port [, hostname] [, backlog] [, callback])

. , IPv6- (::) IPv6 IPv4- (0.0.0.0) . , , .

, , , hostname app.listen() :

app.listen(process.env.PORT || 3000, 'localhost');

3000 localhost(127.0.0.1) . , console.log(server.address()); :

{ address: '127.0.0.1', family: 'IPv4', port: 3000 }

server.listen() nodejs.

0

Source: https://habr.com/ru/post/1612799/


All Articles