I am trying to configure a nodeJS express app which handles a react static build to run from a location inside my VPS (nginx).
So far I have managed to configure the location like this:
added path in webpack build:
output: {
path: path.join(__dirname, '/client/public/dist/'),
filename: 'main.js',
publicPath: "/location/",
},
added the path to the express server:
app.use("/location/", express.static(path.join(__dirname, './client/public/dist/')))
added the location to the react router, to each route
added the location to the nginx proxy_pass:
location /location{
proxy_pass http://localhost:5002/location;
}
The issue I am facing is that if I have a subpath (e.g. /location/page1), if I refresh the browswer I get the error cannot get /location/page1
I have tried to modify the express server as app.use("/location/*", ) or app.use("*"),
but the I get Uncaught SyntaxError: Unexpected token '<'
Is this an issue with index.html? Is there any simpler way to run the app from a location route?
location /location/ {
rewrite ^/location/(.*) /$1 break;
proxy_pass http://localhost:5008/yourPath/;
}
Do not add * path in express, but add the path like above as I have shown in your Nginx file, I have tried this in Nginx itself for my projects.
Here /location/ can be anything of your choice where users can see but in the proxy pass, it should be exactly where you are serving through your application, for example, the route facing the user i.e /location/ can be /test/ that's what users see but after that, the routes can be your /location from where your application is served.
Do let me know if you have ambiguity in understanding the solution I can show you a demo and explain. Thank you.
Related
I have a node.js server that serves the built static files of my create-react-app at /admin. The problem is that anytime I make a change to my react app I have to build the files over again to see the updates. Instead, I'd like to proxy requests for my frontend at /admin to my dev server that comes with create-react-app, running at localhost:3000, because this would allow for a much faster development experience.
This is what my server looks like now:
// app.ts
...
const app: Koa = new Koa();
app.use(mount('/admin', serve(__dirname + '/build')));
...
And this is what I tried:
import proxy from 'koa-better-http-proxy';
const app: Koa = new Koa();
app.use(
mount(
'/admin',
proxy('localhost:3000', {})
)
)
What ends up happening is the requests for static files still go out and the response gives an index.html file but the JS doesn't seem to run and it gives me errors:
Uncaught SyntaxError: Unexpected token '<'
I've also played around with the proxy header settings to adjust content-type to application/json but I had no success there either.
Due to the environment, I cannot just run the node server in one terminal and the react app in another. The request comes from a verified 3rd party and must go through my node server first before being served the frontend portion of my app.
I want to deploy an application that I perform with the MEAN stack on Heroku, but I encounter 1 problem.
I have this folder structure, my node server, with a public folder, where is the dist / fronted folder and all the files generated by Angular's ng build --prod, it works when I start the server and browse normally, but if I refresh the page or write a route myself, I get these errors:
Errores
Sorry for my English.
If your are building a MEAN stack, you probably have a server.js or index.js or app.js as an entry point to your application. An SPA by definition manages all the routes within the router configuration. But if you try to refresh or type a route yourself, it is like you were trying to access that folder on the server (ex: www.mywebsite.com/about, here the folder about might not exist on the server, it is just known by your Angular app)
My suggestion is that you try to add this fix to the app.js (or server.js or app.js) file, so all unexisting routes or refresh go back to your index.html:
// Check your port is correctly set:
const port = process.env.PORT || 8080;
// Is saying express to put everything on the dist folder under root directory
// Check the folder to fit your project architecture
app.use(express.static(__dirname + "/dist"));
// RegEx saying "capture all routes typen directly into the browser"
app.get(/.*/, function(req, res) {
// Because it is a SPA, all unknown routes will redirect to index.html
res.sendFile(__dirname + "/dist/index.html");
});
app.listen(port);
This guy shows full deploy on Heroku with Angular: https://www.youtube.com/watch?v=cBfcbb07Tqk
Hope it works for you!
I am struggling with the deployment of a Vuejs front-end on my local IIS.
I would like to have my app deployed into a folder named FE_STB which is a sub-directory within my C:\inetpub\wwwroot folder.
Ideally, the URL to access the front-end would be http://localhost/FE_STB/.
In order to do so, I tried the following in vue.config.js:
module.exports = {
// Used to build the path for the css, js
baseUrl: process.env.NODE_ENV === 'production'
? '/FE_STB/'
: '/',
// The folder where the app will be built in the project directory instead of the default dist folder
// outputDir: 'Vue',
};
running npm run build generates an index.html, a favicon.ico, my img, js, css and fonts folders.
The index.html contains link tags such as (<link href=/FE_STB/css/chunk-05c5.672f5bfa.css />) and i thought it was going in the good direction.
However, it keeps returning a
404 not found error
when i try to access http://localhost/FE_STB/.
On the other hand, If I copy only the index.html into the root directory of my IIS installation (wwwroot) instead of the FE_STB subdirectory, and check the http://localhost/ URL, my app appears correctly.
However, when I start browsing the app and hit the refresh button, I get an error. For example, If I am on http://localhost/about/ on my app and refresh it with F5, I will get a 404 error as it’s looking for C:\inetpub\wwwroot\about\ directory which doesn’t exist obviously.
I also tried the web.config and the IISrewrite solutions as explained on the vuejs website or tried to add:
const router = new VueRouter({
mode: 'history',
// To define sub-directory folder for the deployment
base: 'FE_STB',
routes,
linkActiveClass: 'active',
scrollBehavior(to, from, savedPosition) {
return savedPosition || { x: 0, y: 0 };
},
});
in my router.js but it doesn’t change anything.
Any tips or directions would be really helpful.
Thank you
S.
I have made a React app which is hosted on the same machine as my API server as guided by this tutorial.
My directories are set up in such way that
- root
- server.js (entry for the API server)
- app (for all back-end stuff)
- client (directory for React app)
- public (static files used in the front-end)
- src (for all front-end stuff)
- build (directory containing static build)
- index.html (entry for front-end)
- ... (other bundled stuff)
And I am serving a static copy of my React app in below method.
// server.js
const app = express();
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
app.use('/api', routes); // for API routes
I am having a problem where, when I boot this up locally, (with NODE_ENV=production) everything smooth and page refreshes do not break the app.
However, after I have deployed this to Elastic Beanstalk, page refreshes break the app with this html error message being displayed in the browser.
Cannot GET /{whichever url path I had prior to refresh}
I could see in the logs that browser tried to send a request to the server with GET /{url_pathname}
I initially suspected something going funny with having both React router and Express router but then again, I am confused why this is not consistent with the case in the localhost.
If you are using NGINX, you need to update the configuration file located at
/etc/nginx/sites-available
location / {
try_files $uri $uri/ /index.html$is_args$args;
}
Or this if your app is not located in the default directory, for example "app-location" you would get this.
location /app-location {
root /var/www;
index index.html;
try_files $uri $uri/ /app-location/index.html;
}
And restart nginx
sudo service nginx restart
If you are running a react app that basically runs on a single index file, then your middleware router should have a catch-all route that always points to that index file somewhere.
app.get('/*',(req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'))
})
Please note that the current example is dependent on the path module...So:
import path from 'path'
// or
const path = require('path')
I am currently serving angular1 application over node js app.
I want to serve my angular1 app as well as angular2 app from same domain with this node server.
I have following setting in my app.js of node js file:
var staticDir = nodeEnv == 'development' ? path.join(__dirname, '/home/sovf/ang1/') : '/data/project/angular/app';
My angular1 project directory is here: /home/sovf/ang1/
My angular2 project directory is here: /home/svof/ang2/
How can I configure the staticDir to use '/home/svof/ang2/' , when the url matches /ang2/*
I have tried enogh to look for resources and node configuration for this but could not find anything. Can any one please help here.
You can create a new instance of express.static that uses /home/svof/ang2 as root directory (where it should look for the static resources), and "mount" that instance on /ang2, so it will only match requests that start with that prefix:
app.use('/ang2', express.static(path.join(__dirname, '/home/svof/ang2')));