0

#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("Example app listening at http://%s:%s", host, port)
})

this is my server.js code from which I am calling the user.json file for the data

#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
 }
}

This is my file in which my list of user are there

But when I run the app I didn't get any data

Jagan Kaartik
5902 gold badges7 silver badges16 bronze badges
asked May 26, 2021 at 8:38
1
  • 2
    You don't need to use fs.readFile. You can simply require it and it will be read properly as a JSON and an object will be created. Then you can simply send that object. Commented May 26, 2021 at 9:06

3 Answers 3

3

First of all use path when you are trying to get a file path.

 path.resolve(__dirname, "users.json");

Second of all add error handling, because you may receive an error and that is why you see no data.

 fs.readFile(path.resolve(__dirname, "users.json"), 'utf8', function (err, data) {
 if (err) {
 res.status(500);
 res.end(err.message);
 console.error(err);
 return;
 }
 console.log( data );
 res.end( data );
 });
answered May 26, 2021 at 8:48
Sign up to request clarification or add additional context in comments.

2 Comments

please fix the file name. it is "users.json" and not "users.jso"
Thanks! I have fixed the name
0

This should make it work as required,

  • Added ES6 Syntax
  • Added Error Handling
const express = require('express');
const app = express();
const fs = require("fs");
app.get('/listUsers', (req, res) => {
 fs.readFile('user.json', (err,data) => {
 if(err) res.status(500).send(err.message)
 else {
 let displayData = JSON.parse(data);
 //console.log(displayData)
 res.status(200).send(displayData);
 }
 });
})
app.listen(8081,() => console.log("Example app listening at port 8081"))

Without using fs

const user = require('./user.json')
app.get('/listUsers', (req, res) => {
 res.send(user);
})
answered May 26, 2021 at 9:01

1 Comment

Using requires is not the best solution, because we require it once and then it lives in the memory so any changes to the file won't make it to the running program.
-2

Try this one:

fs.readFile( __dirname + "/user.json", 'utf8', function (err, data) {
 console.log( data );
 res.end( data );
 });
  1. You probably just misstyped file name - you have users.json instead of user.json

  2. Add / before file name

  3. Add error handling.

answered May 26, 2021 at 9:06

3 Comments

try-catch block won't catch async errors.
The truth is that the example you are showing is not use async/await and the only way to handle the error in this example will be checking err parameter of the callback.
Edited my answer. Anything else?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.