Routes in express JS - node.js

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).

Related

Is it possible to give a custom path to the `/static` directory using React and Express?

Edit
To anyone coming here in the future, I solved this problem by indicating a value for PUBLIC_URL in my .env file.
In my case, I set PUBLIC_URL=/ui and now the static assets are being served from .../ui/static/...
Whenever I build my app and launch it, the browser requests content from the static directory, such as this:
Is it possible to somehow change this? I took a look here, but I didn't have any luck. I tried different combinations of express.static(. . .) to no avail.
Instead of http:///static/bundle123, I want it to request http:///custom/static/bundle123.
I've tried moving my build/static directory into build/custom/static and doing something like this, but it didn't work:
app.use('/static', express.static(path.join(__dirname, './custom')))
What am I doing wrong?
The first argument determines the URL path and the second part determines where the files are located on the file system.
So if your static files are located in the public directory (or in sub directories) and you want to access then on the https://.../static URL, you can use:
app.use('/static', express.static(path.join(__dirname, 'public')));
In your case, assuming you run the server from the build directory, using the following should work:
app.use('/custom/static', express.static(path.join(__dirname, 'static')));
Now if you request an URL https:///custom/static/foo Express will look for a file named foo in the static folder.

Nodejs file direct access in browser

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.

Changing connect-assets lookup path

If I execute my expressjs app like so node app.js everything works fine however when I execute it like node path/app.js it starts looking for public assets from the execution location.
How can I change the path?
app.use(express.static(path.join(__dirname, 'public')) doesn't seem to be doing anything
The error I get:
Asset 'styles.css' not found in search path:
/path/public/css
/path/public/js

how to set upload path in express4?

I want to make express serve files requested as http://localhost/uploads/image.png serve the files out of a dynamic directory (based on environment) process.env.NODE_UPLOAD_DIR which would be something like:
/home/user/data/uploads but the app is served out of ~/www/domain.com.
Is this possible?
I tried this, but it just redirects to homepage in browser when I request it (its an angular app if that makes any difference):
app.use(express.static(path.join(proces.env.NODE_UPLOAD_DIR, 'uploads')));
If you want the "uploads" portion in both the URI and the filesystem path, you need to use it as a prefix:
app.use('/uploads', express.static(
path.join(process.env.NODE_UPLOAD_DIR, 'uploads'))
);

How to find the relative path to my javascript in node/express?

I have a node/express app with:
.set('views', __dirname + '/dist')
dist has a subdirectory guestApp.
in dist/guestApp I have an html page guest.html and a JavaScript file: vendor.js.
The html page has a script tag with: src='vendor.js'
I render the page with: response.render('guestApp/guest.html', {activeProfile: 'Me', title: 'World'});
The page is rendered, but the JavaScript is not found. I have tried all sorts of combinations but I can't seem to get it right. What is the correct relative path to that script please?
If your html file is not dynamic you don't need to set views. You have to setup the static middleware.
app.use(express.static(__dirname+'/dist'));
And the javascript file should also be referenced relative to the dist directory -
src='guestApp/vendor.js'

Resources