How to Use the fs Module with React JS Website (Launch a 2nd Node Server)

Опубликовано: 19 Апрель 2023
на канале: ZeroLife
2,612
8

=========================
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


Смотрите видео How to Use the fs Module with React JS Website (Launch a 2nd Node Server) онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь ZeroLife 19 Апрель 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 2,612 раз и оно понравилось 8 людям.