how to handle client side react routing in loopback.js - reactjs

I am using react with loopback. I wanted to integrate react code in loopback.
if i do these 3 steps
1)middleware.json - put this
"files": {
"loopback#static": {
"params":"$!../client"
}
},`
2)root.js
router.get('/');
3)front end code
"build": "react-scripts build && cp -r build/* ../client/",
That didopen my react site on localhost:3000 .Now issue is when i do this
i cant access my loopback on :3000/explorer
So my first question is in this scenario, how to access explorer.
But then i rolled it back , because i wanted to use loopback explorer again.
So, i deleted all these added code and explorer came back
but when i added it again
Now, i dont see my react code
I can still see explorer at http://localhost:3000/explorer/
if i go to http://localhost:3000/apphome
i see 404 error
Right now, my middleware.json file for loopback is
{
"initial:before": {
"loopback#favicon": {}
},
"initial": {
"compression": {},
"cors": {
"params": {
"origin": true,
"credentials": true,
"maxAge": 86400
}
},
"helmet#xssFilter": {},
"helmet#frameguard": {
"params": [
"deny"
]
},
"helmet#hsts": {
"params": {
"maxAge": 0,
"includeSubdomains": true
}
},
"helmet#hidePoweredBy": {},
"helmet#ieNoOpen": {},
"helmet#noSniff": {},
"helmet#noCache": {
"enabled": false
}
},
"session": {},
"auth": {},
"parse": {
"body-parser#json": {},
"body-parser#urlencoded": {
"params": {
"extended": true
}
}
},
"routes": {
"loopback#rest": {
"paths": [
"${restApiRoot}"
]
}
},
"files": {
"loopback#static": {
"params":"$!../client"
}
},
"final": {
"loopback#urlNotFound": {},
"./LoopbackUrlNotFoundCatch.js": {}
},
"final:after": {
"strong-error-handler": {}
}
}
root.js file
'use strict';
//router.get('/', server.loopback.status());
module.exports = function(server) {
// Install a `/` route that returns server status
var router = server.loopback.Router();
router.get('/');
server.use(router);
};
-edit
I made some changes. Now, i have react components showing, I can also see data when i use api routes. But, explorer is still missing.
middleware.json
"files": {
"loopback#static": [
{
"name": "publicPath",
"paths": ["/"],
"params": "$!../client"
},
{
"name": "reactRouter",
"paths": ["*"],
"params": "$!../client/index.html",
"optional":true
}
]
},
I have also changed named of root.js to root_something.js . In documentation, its written, no need of root.js

First of all you Should be create react app in an other director like
Root ->
|-- client // emply
|-- clint_src // react app
and getting build react app and copy build files to client
now you should be remove server.loopback.status() in "server/boot/root.js" file
Example:
router.get('/', server.loopback.status());
To :
module.exports = function(server) {
// Install a `/` route that returns server status
var router = server.loopback.Router();
router.get('/');
server.use(router);
};
after that you need say to loopback middleware which file should be load in your path
go to middlware /server/middleware.json and replace blow code to "files": {}
"files": {
"loopback#static": [
{
"paths": ["/"],
"params": "$!../client"
},
{
"paths": ["*"],
"params": "$!../client"
}
]
},
on "paths": ["*"], all route will be open react file exeption the "/api" and "explorer" so you should be handle page notfound inside react app
Hope This was help full
Good Luck

I suspect React's default service worker intercepts and tries to cache /explorer. It skips urls prefixed with __ so this might fix the need to clear browser:
In component-config.json:
{ "loopback-component-explorer": {
"mountPath": "/__explorer" }}
Then access explorer at /__explorer.

As the instructions for adding a GUI to a loopback application state, you need to remove the root.js '/' route completely, by either renaming the root.js file to something without a .js extension, or deleting the file altogether.
$ rm root.js
### or, you can do this
$ mv root.js root.js.old
In the loopback 3 server configuration, the client folder has to be setup as a middleware route in middleware.json, like so:
"files": {
"loopback#static": {
"params": "$!../client"
}
},
Now, your client application is served from the /client folder, and by default the static files are served with Express -- so, it will look for index.html when you hit localhost:3000/

Related

How can I make my app made with react js and vite, to a PWA?

I've been reading a bit about it but I don't know how to configure correctly to convert my reactjs website to PWA
I've created a workbox-config.js file at the root of my project, and a manifest.json file at the top of my App.js, but I don't know what else to do
The content of the workbox-config.js file is:
module.exports = {
"globDirectory": "dist/",
"globPatterns": [
"**/*.{html,js,css,svg,png,jpg,jpeg,gif,webp}"
],
"swDest": "dist/sw.js",
"clientsClaim": true,
"skipWaiting": true,
"navigateFallback": "/index.html",
"navigateFallbackWhitelist": [/^\/(about|contact)$/],
"runtimeCaching": [
{
"urlPattern": /^https:\/\/fonts\.googleapis\.com/,
"handler": "StaleWhileRevalidate"
},
{
"urlPattern": /^https:\/\/my-api\.com/,
"handler": "NetworkFirst",
"options": {
"cacheName": "api-cache",
"networkTimeoutSeconds": 10
}
}
]
};

Jest cannot find module #env React Native

I'm currently facing some problem I'm using in my react native app https://github.com/goatandsheep/react-native-dotenv for handling .env.
Error => Cannot find module '#env' from 'src/api/api.ts'
I'm testing currently my redux saga to call the api endpoint:
import axios from 'axios';
import {API_URL} from '#env';
export default axios.create({
baseURL: API_URL,
responseType: 'json',
});
API Endpoint
export const sendCheckNumber = async (
phoneNumber: string,
): Promise<AuthenticationTO> => {
const response = await axios.get<AuthenticationTO>(
`SendCheckNumber/${phoneNumber}`,
);
return response.data;
};
I'm using ts-jest package.json. I saw in the docs its possible to include bable in ts-jest because I'm using bable to import the 'module:react-native-dotenv', as plugin. I thought that will already solve my problem but unfortunately it still fails. Maybe someone of you have some suggestion what could cause this error.
Thank you!!!
package.json
"jest": {
"preset": "react-native",
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js",
"\\.(ts|tsx)$": "ts-jest"
},
"globals": {
"ts-jest": {
"babelConfig": "babel.config.js",
"tsConfig": "tsconfig.jest.json"
}
},
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"modulePaths": [
"<rootDir>/src"
],
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$"
}
Maintainer goatandsheep here. I recently added some documentation on how this is done. Take a look at the documentation section on TypeScript. Here is the excerpt:
Create a types folder in your project
Inside that folder, create a *.d.tsfile, say, env.d.ts
in that file, declare a module as the following format:
declare module '#env' {
export const API_BASE: string;
}
Add all of your .env variables inside this module.
Finally, add this folder into the typeRoots field in your tsconfig.json file:
{
"typeRoots": ["./src/types"],
}
Let me know if that works!
I fixed this problem like following.
import {API_URL} from '#env';`
Added this instead:
import {API_URL} from 'react-native-dotenv';
And add the next in babel.config.js
module.exports = function (api) {
api.cache(true)
return {
presets: ["babel-preset-expo"],
plugins: [
[
"module:react-native-dotenv",
{
moduleName: "react-native-dotenv",
},
],
],
}
}
It works well for me.

Chrome Extension Reactjs chrome.tabs

I'm trying to build a chrome extension using React and I managed to get my manifest.json set up as such:
{
"short_name": "GitMap",
"name": "GitMap",
"manifest_version": 2,
"browser_action": {
"default_popup": "index.html",
"default_title": "GitMap"
},
"permissions": [
"tabs"
],
"version": "1.0"
}
. I want to access the url of the currently active tab using chrome.tabs.query({ "currentWindow":true}, function(tab) { blahblahblah}
within my React application's app.js file, but i'm receiving this error:
TypeError: Cannot read property 'query' of undefined.
How do i access the 'chrome' method from within a react file? Thank you
try this solution
chrome.tabs.getCurrent(function(tab){console.log(tab.url);});

How to configure index.jsp file with app.json in Extjs 6

Structure of my Application:
In my app.json file I did following configuration for pointing to index.jsp file
"indexHtmlPath": "../../iris_app.war/WEB-INF/views/jsp/app/index.jsp",
"output": {
"base": "${workspace.build.dir}/${build.environment}/${app.name}",
"page": {
"path": "index.jsp",
"enable": true
},
"manifest": "${build.id}.json",
"js": "${build.id}/app.js",
"appCache": {
"enable": false
},
"resources": {
"path": "${build.id}/resources",
"shared": "resources"
}
},
When I refresh my app using sencha app refresh it updates classic.json file
With following paths
{"paths":
{
"Dimension":"../../../../../iris_s.war/regshoapp/app/view/components/popups/SelectDimensionsWindow.js",
"Ext":"../../../../../iris_s.war/ext/classic/classic/src",
"Ext.AbstractManager":"../../../../../iris_s.war/ext/packages/core/src/AbstractManager.js",
"Ext.Ajax":"../../../../../iris_s.war/ext/packages/core/src/Ajax.js"
….etc.
When I deployed this application on server and run on browser then I use this
url
localhost:7001/iris_ops_app/
When I run application on browser it throws file not found error on console for each Ext File which is mentioned in “classic.json” file but these files exist under “http://localhost:7001/iris_ops_app/regshoapp” path.
Please let me know how can I resolve this path issue on browser. Actually “iris_s.war” should be replaced by “iris_ops_s/regshoapp” in “classic.json” file only then it will resolve all paths.

using socket.io in sencha touch application

I want to use socket.io in my sencha touch application
I added socket.io in my app.json js array :
"js": [
{
"path": "touch/sencha-touch.js",
"x-bootstrap": true
},
{
"path": "bootstrap.js",
"x-bootstrap": true
},
{
"path": "lib/socket.io.js"
},
{
"path": "app.js",
"bundle": true, /* Indicates that all class dependencies are concatenated into this file when build */
"update": "delta"
}
here is where I want to create var socket :
var socket = io.connect('http://localhost:3000');
when I open index.html in browser I get this error :
ReferenceError: Can't find variable: io
how can I fix my application?

Resources