I am working on a react application that takes the json file path as a parameter to render the json data in my ui. Accessing local files from the browser is restricted, so how can I create a backend server to retrieve my local json files and serve them to the browser?
To access local json files in your swagger application, you need to use express - as the browser cannot access your local file system. You can create an endpoint, i.e. '/swagger', that will allow you to serve the files from the directory provided. In the urls parameter, you will use '/swagger/name.json', rather than the local path. Create a driver.js file with the following content:
var express = require('express');
var app = express();
app.use('/swagger', express.static('/path/to/local/files'));
app.listen(3000);
you can boot up a local server using express, and use the fs module to access file content
const fs = require('fs');
const file_content = fs.readFileSync('./{file_name}',
'{content_formate}').toString();
// For show the data on console
console.log(file_content);
To create a server that is listening on port 3000, use
const express = require('express')
const fs = require('fs');
const app = express()
const port = 3000
app.get('/', (req, res) => {
const file_content = fs.readFileSync('./{file_name}',
'{content_formate}').toString();
res.send(file_content)
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
something like this