How to do I fix this Node.js error.
const express=express();
const app=express();
const fs=require("fs");
const movies=JSON.parse=fs.readFileSync("movies.json", "utf-8"); // this line of code returns error.
app.get("/api/vi/movies", (req, res)=>{
res.status(200).json({
status: "success",
data: {
movies: movies
}
})
})
app.listen(3000, "127.0.0.1", ()=>{
console.log("Connecting ...");
});
All the code has been written above.
2
Answers
Did you mean:
const movies=JSON.parse(fs.readFileSync("movies.json", "utf-8"));
instead of
const movies=JSON.parse=fs.readFileSync("movies.json", "utf-8");
?For future reference, you should probably put your error in the post instead of linking to elsewhere.
Parse the data into Json.parse(), which returns from the fs.readFileSync() method.