=========================
1) Install some dependencies:
=========================
$ npm install --global yarn
$ yarn add concurrently
$ yarn add wait-on
$ yarn add cross-env
========================================================
2) Edit your package.json to add the following lines under "scripts":
========================================================
"web:serve": "concurrently -k \"cross-env BROWSER=none node {PATH_TO_YOUR_JS_API_SCRIPT}\" \"yarn web:start\" ",
"web:start":"wait-on http://127.0.0.1:3001 && yarn start"
==================
3) Your API JS script:
==================
const express = require('express');
const app = express();
var fs = require("fs");
// Use the following header for the page
app.use((req, res, next)=❯{
res.header("Access-Control-Allow-Origin","*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Entry page
app.get('/', (req, res) =❯ {
res.send('trackerR324')
});
// Using the fs module
app.get('/PATH1', (req, res) =❯ {
res.send(fs.readFileSync('C:\\example\\example.txt',{encoding:'utf8'}))
});
// Start the server on port 3001
app.listen(3001)
====================================
4) Use a fetch request to connect to your API
====================================
async function someFunc(){
let promise1 = await fetch("http://127.0.0.1:3001/PATH1");
let promise2 = await promise1.text();
let promise3 = await JSON.parse(promise2);
};
=======================
5) Run in the Terminal
=======================
$ yarn web:serve
DONATE:
---------------
PAT: / rezatahirkheli
PP: https://www.paypal.com/paypalme/rezat...
BTC: 3EUQBWZKX9Vcwdffd3cUGATQopxrAQQxJ9
LTC: MBXE6hJgxxFYSD8SjmR7sHCHGTqFXmNGoU
Watch video How to Use the fs Module with React JS Website (Launch a 2nd Node Server) online without registration, duration hours minute second in high quality. This video was added by user ZeroLife 19 April 2023, don't forget to share it with your friends and acquaintances, it has been viewed on our site 2,612 once and liked it 8 people.