NodeJs / ExpressJs URLs - node.js

I need to parse parameters in the URL that come in this form:
localhost:8080/p/a=12345&b=acbd
After the variables are read, I load a HTML file that is in the public folder of my express server. That works ok. The problem is that my HTML file load several JS and CSS files and since the path is localhost:8080/p/ the files with relative paths can't be found.
What I need is something similar to the apache URL rewrite, where I can send the traffic to a specific file and change the URL to look in the way I want. I haven't found a node module that does that, any suggestions?

If you are using the static middleware to serve your static files, you can use
app.use('/p', express.static(__dirname + '/public'));
instead of
app.use(express.static(__dirname + '/public'));
You can see more details in the document of the Express

Related

Can someone please explain how static files in express js work?

I am new with nodejs and much more new with express module. I have a app setup like this;
The chart.js is my nodejs file. I am trying to make my js files and css file static by using app.use(express.static(-I didn't understand what i need to write here-)) in order to render my index.html properly but I don't know how to use and I did not understand the documentation. In the documentation they say coder able to use static like app.use(express.static('public')) but they don't mention about what is public, where it is in the project, what does it contain. Can someone please help mi about this situation? How does this express.static works and how can I make my files static?
NOTE: DO NOT PUT PRIVATE FILES INSIDE YOUR STATIC FOLDER.
app.use(express.static(__dirname + '/public'));
here you see inside of express.static() function is the path of your static folder that will be going to access directly from the browser and you don't need to write their routes because that folder will give all the access to the public. like css,js files. and those files you will be able to access as its directory.
in the above picture, you have html, css and js files in public folder which is located on root folder of the application. you need to access those public static files which are not related to nodejs so it should be defined as static on your server node js code as : app.use(express.static(__dirname + '/public'));. and it will get all the routes like:
http://localhost:3000/css/style.css
http://localhost:3000/javascript/script.js
http://localhost:3000/favicon.ico
http://localhost:3000/index.html
http://localhost:3000/robots.txt
you also can set prefix for those static routes. for that, you need to give prefix as: app.use('static_folder', express.static(__dirname + '/public')); then it will be looks like :
http://localhost:3000/static_folder/css/style.css
http://localhost:3000/static_folder/javascript/script.js
http://localhost:3000/static_folder/favicon.ico
http://localhost:3000/static_folder/index.html
http://localhost:3000/static_folder/robots.txt

Node Express force all static routes to point to root path

I have a node.js application that I am trying to genericize so that no matter the URL on the back end it always uses the root path to serve static files.
So currently my app sits at https://myapp.heroku.com
I have some js, css, images and a few other static files.
What I want is when the url looks like https://myapp.heroku.com/brand/somebrand that the files that are served are served form the root as well including any internal references inside said files.
I have ensured that all paths begin with a '/' and have also tried the following in every combination possible.
app.use('/', express.static(__dirname + '/public', options));
app.use('*', express.static(__dirname + '/public', options));
app.use(express.static(path.join(__dirname, 'public')));
am I on the right track?
This app is actually being loaded by another system which may actually be the problem, unfortunately I cannot debug on that system so I need to rule this out first.

nodejs express not to intercept messages for a folder with static content

I've a node, express system installed working on a host.
All requests are going through in the app.get('/path'... format
however in the domain I've html folder with static content that I want to serve
http://domain.com/html/attendee
http://domain.com/html/sponsors
and don't want node/express to intercept these requests and let them go through directly, not even serve through nodejs, otherwise relative linking problem.
Please suggest a solution.
You can't do it that way. node doesn't serve ANY content by default - it is not like some other web servers in that regard.
Instead, you specifically configure express to serve content from certain paths directly from the file system by inserting the right middleware commands early in the middleware stack.
For example, in one of my node apps, I use this middleware:
// static routes that get no further processing
app.use('/img', express.static(__dirname + '/img'));
app.use('/lib', express.static(__dirname + '/lib'));
This tells express that any content that starts with "/img" should be served directly from the appDirectory + "/img" directory. Same for elements in "/lib". One nice thing about this is that the paths you expose to the outside world do not have to be the same as the paths you use on your server and, in fact, by changing a few characters in your code, you can easily map to different directory.

Getting Started with express.js, serving a dynamic HTML

I'm writing a single page web application using node.js and express. After going through some online tutorials, I found out this is how express.js serves a client directory with HTML, javascript and css
app.use(express.static(__dirname + '/public'));
This works great except the fact that public directory has to have .html root file which is static. I'd like to know how can I serve a dynamic HTML file with this same approach.
I want to insert some data into the page for example "Hello user_name" at the top. How do I do that for this single file.
I want to only do it once, at the startup, after that my application will make rest API calls and get plain JSON responses which it will then put it in the page.
You cannot use the express static server to serve dynamic files as far as I'm aware. In order to serve a dynamic HTML you need to use a templating engine, such as jade or any of these templating engines.
Jade is pretty straightforward to use and is supported by express by default. To set up jade, include the following lines in your app configuration block:
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
The first line sets your views directory - from which the template files would be served. Since you want to serve only an index file, this directory would contain your index.jade template. The second line sets jade as the templating engine.
Now, instead of using the static server to serve the index.html, you need to create a get route '/' to serve the index file, like so:
app.get('/', function(req, res) {
res.render('index', {user_name: username});
});

Node.js change url base with prefix

I have a node.js app with express and jade templates.
Just now I've running like any-host:8000 but I need to change to any-host:8000/web/
but this causes change all href and location css,img,js...
Any idea to do something to achieve transparently
I've tried with:
app.namespace('/admin', function(){...}
but then I need change the href of all the html links in the app.
Any suggestions?
app.use('/urlbase', express.static(__dirname + '/public'));
(assuming your app is located in myapp/).

Resources