I have build an application with NodeJs + ExpressJs.
I have file jquery in folder js of public, ex:
http://192.168.2.129:7878/javascripts/jquery.min.js
But how can I config setting Express to get this file jquery when i get this url
http://192.168.2.129:7878/1111/javascripts/jquery.min.js
From documentation, to add relative 'virtual' path you write following code:
app.use('/1111', express.static('public'));
Related
At Plesk server there are nodejs and reactjs build on hit url the build run but when we hit the nodejs file url of js files it open directly on browser means nodejs files are not secure.
So, it sounds like you are using express.static(), yet the user is able to fetch your server files that are not meant to go to the client. That apparently means that you've pointed express.static at a directory that contains your server files. Instead, you need to point express.static() at a directory hierarchy that ONLY contains files meant to be sent to the client. That means it has to be a separate directory from your server files and it has to not be above your server files directory.
There are many possible places to put it. Here are a couple ways to organize things:
projectDirectory
serverFiles
server.js
clientFiles
index.html
login.html
Then, when running server code from the serverFiles directory, you would use an express.static() like this:
const path = require('path');
app.use(express.static(path.join(__dirname, "../clientFiles")));
Or, you can do it like this:
projectDirectory
serverFiles
server.js
clientFiles
index.html
login.html
const path = require('path');
app.use(express.static(path.join(__dirname, "clientFiles")));
The idea is that the clientFiles directory hierarchy contains only client-side files and express.static() by default will not allow ../ syntax in the URLs to go above it.
I have a vuejs frontend and an express nodejs backend. But I don't know how I can deploy both of them to the same Amazon EC2 instance with the same domain name pointing to them. Please can anyone help me with this? Or suggest a better way of doing this?
You can merge two repos and deploy both backend and frontend as follows
Inside your nodejs app, open a folder named client and put all the Vue project inside it.
If you are using Vue CLI, change your vue.config.js as follows to create a dist folder inside the root of the nodejs project like
const path = require('path');
module.exports = {
outputDir: path.resolve(__dirname, '../dist'),
};
Make all the get/post endpoints of nodejs application start with /api/ to not get conflict with the path that redirects all the requests to vue client app other than /api/ paths.
Run npm run build to create a dist folder inside nodejs root backend folder
If you are using express.js, serve dist folder with nodejs express backend like;
index.js
const express = require('express');
const app = express();
// Serve Vue Dist Folder
app.use(express.static(__dirname + '/dist'));
app.get('*', (req, res) => res.sendFile(__dirname + '/dist/index.html'));
nodejs with e.g. express can also serve static content. Just put your vuejs files in a static folder.
I working on Angular application, where I am trying to server the build files from my Angular application through the dist directory in my express. To do this I copy the files generated by ng build and paste them into dist folder of express.
My dist folder looks like below
I am using the following code to serve the index.html file
this.app.use(express.static(__dirname + '/dist'))
But seem to be getting "Cannot GET /" error
Similarly if I do
this.app.use(express.static(__dirname + '/public'));
it serves the html in the public folder.
As far as my understanding, you can serve static from any folder in express. I am not sure if I am missing something here.
Make new directory ./dist/www and move your files except server.js to here.
this.app.use(express.static(__dirname + '/www'));
It looks like you should replace
this.app.use(express.static(__dirname + '/dist'))
with
this.app.use(express.static(__dirname))
I am using express with webpack-dev-middleware and webpack-hot-middleware in my development environment. I build a storage/DFS module that works in a similar way as GitHub. You paste a URL and it opens up a folder or a file. The problem now that I have is, when I paste a URL that contains a file extension at the end as in (/storage/folder1/folder2/file.py) I get 'Cannot GET /storage/folder1/folder2/file.py'.
It is a VUE app and for routing I am using vue-router. The path configured in VUE app is:
path: 'storage/*'
Using as a template for VUE app: https://admin.vuebulma.com/#/
Any suggestions on how fix this?
I have a code line:
app.use(express.static('public'));
for static files in public folder, but build a route:
app.get('/search/jobs', jobs.index);
The Expressjs is putting /search before url.
And I'm a getting error in console browser:
GET: http://localhost:5000/search/css/materialize.css
Any idea?
You need to use absolute paths in your html/css (e.g. /css/materialize.css). With relative paths (e.g. css/materialize.css) the browser will look up the path relative to the current path/"directory" (/search in this case).