#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
pal deepak
651 gold badge1 silver badge7 bronze badges
3 Answers 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
Ayzrian
2,4751 gold badge9 silver badges16 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Krushn Dayshmookh
please fix the file name. it is "users.json" and not "users.jso"
Ayzrian
Thanks! I have fixed the name
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
Jagan Kaartik
5902 gold badges7 silver badges16 bronze badges
1 Comment
Ayzrian
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.
Try this one:
fs.readFile( __dirname + "/user.json", 'utf8', function (err, data) {
console.log( data );
res.end( data );
});
You probably just misstyped file name - you have users.json instead of user.json
Add / before file name
Add error handling.
3 Comments
Ayzrian
try-catch block won't catch async errors.
Ayzrian
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.Andrii
Edited my answer. Anything else?
lang-js
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.